Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e1f23da

Browse files
author
Joel M
committed
Initial Commit.
1 parent 7f657dd commit e1f23da

File tree

1 file changed

+38
-8
lines changed

1 file changed

+38
-8
lines changed

cachematrix.R

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,45 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
3-
4-
## Write a short comment describing this function
1+
## cachematrix() contains convenience functions for
2+
## matrix storage and inversion. The matirx 'x' will have
3+
## its inverse calculated once. If the inverse has already
4+
## been calculated (and the matrix has not changed), then
5+
## the cachesolve() should retrieve the inverse from the cache.
56

7+
## This function creates a special "matrix" object
8+
## that can cache its inverse
69
makeCacheMatrix <- function(x = matrix()) {
7-
10+
im <- NULL
11+
12+
set <- function(y){
13+
x <- y
14+
im <<- NULL
15+
}
16+
17+
get <- function() x
18+
setmatrix <- function(solve) im <<- solve
19+
getmatrix <- function() im
20+
21+
list( set=set, get=get,
22+
setmatrix=setmatrix,
23+
getmatrix=getmatrix)
824
}
925

1026

11-
## Write a short comment describing this function
12-
27+
## This function computes the inverse of the
28+
## special "matrix" returned by makeCacheMatrix above.
29+
## If the inverse has already been calculated (and the matrix
30+
## has not changed), then the cachesolve should retrieve the
31+
## inverse from the cache.
1332
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
33+
## This function assume that the matrix supplied is
34+
## always invertible.
35+
im <- x$getmatrix()
36+
if(!is.null(im)){
37+
message("getting cached data...")
38+
return (im)
39+
}
40+
m <- x$get()
41+
im <- solve(m, ...)
42+
x$setmatrix(im)
43+
## Return a matrix that is the inverse of 'x'
44+
im
1545
}

0 commit comments

Comments
 (0)