Skip to content

Commit 5f29907

Browse files
committed
coded functions for a cached inverse for a matrix
1 parent 7f657dd commit 5f29907

File tree

1 file changed

+31
-3
lines changed

1 file changed

+31
-3
lines changed

cachematrix.R

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,43 @@
11
## Put comments here that give an overall description of what your
22
## functions do
33

4+
45
## Write a short comment describing this function
6+
# makeCacheMatrix create an R object which calculates the matrix^-1 only once
57

68
makeCacheMatrix <- function(x = matrix()) {
7-
9+
ix <- NULL
10+
set <- function(mx) {
11+
x <<- mx
12+
ix <<- NULL
13+
}
14+
get <- function(){
15+
x
16+
}
17+
setinverse <- function(inverse) {
18+
ix <<- inverse
19+
}
20+
getinverse <- function() {
21+
ix
22+
}
23+
list(set = set
24+
, get = get
25+
, setinverse = setinverse
26+
, getinverse = getinverse)
827
}
928

1029

11-
## Write a short comment describing this function
12-
30+
# returns the inverse matrix from the object obtained from
31+
# makeCacheMatrix calculating the value only once
1332
cacheSolve <- function(x, ...) {
1433
## Return a matrix that is the inverse of 'x'
34+
i <- x$getinverse()
35+
if(!is.null(i)){
36+
return(i)
37+
}
38+
mx <- x$get()
39+
i <- solve(mx, ...)
40+
x$setinverse(i)
41+
i
1542
}
43+

0 commit comments

Comments
 (0)