Skip to content

Commit 97450d4

Browse files
committed
commit rdpeng#1
1 parent 7f657dd commit 97450d4

File tree

1 file changed

+42
-2
lines changed

1 file changed

+42
-2
lines changed

cachematrix.R

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,55 @@
1+
##setwd("C:/Users/eduardo/Documents/GitHub/ProgrammingAssignment2")
12
## Put comments here that give an overall description of what your
23
## functions do
34

45
## Write a short comment describing this function
56

67
makeCacheMatrix <- function(x = matrix()) {
8+
d <- dim(x)
9+
if (d[1] != d[2]) {
10+
stop("The matrix has to be square")
11+
}
712

13+
## matrix of inverse
14+
i <- NULL
15+
16+
## set matrix
17+
set <- function(y) {
18+
x <<- y
19+
i <<- NULL
20+
}
21+
22+
## get matrix
23+
get <- function() x
24+
25+
## set matrix of inverse
26+
setmi <- function(matrix) i <<- matrix
27+
28+
## get matrix of inverse
29+
getmi <- function() i
30+
31+
32+
list(set = set, get = get,
33+
setmi = setmi,
34+
getmi = getmi)
35+
836
}
937

10-
1138
## Write a short comment describing this function
1239

1340
cacheSolve <- function(x, ...) {
41+
42+
i <- x$getmi()
43+
if(!is.null(i)) {
44+
message("getting cached data")
45+
return(i)
46+
}
47+
48+
matrix <- x$get()
49+
i <- solve(matrix)
50+
x$setmi(i)
51+
1452
## Return a matrix that is the inverse of 'x'
15-
}
53+
i
54+
}
55+

0 commit comments

Comments
 (0)