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

Skip to content

Commit 8055cfd

Browse files
author
Oleg Atamanenko
committed
Implemented cache matrix assignment
1 parent 7f657dd commit 8055cfd

File tree

1 file changed

+29
-7
lines changed

1 file changed

+29
-7
lines changed

cachematrix.R

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,37 @@
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+
## Matrix inversion is usually a costly computation and there may be some
2+
## benefit to caching the inverse of a matrix rather than compute it repeatedly
3+
## (there are also alternatives to matrix inversion that we will not discuss
4+
## here).
55

6+
## Create a special "matrix" object that can cache its inverse.
67
makeCacheMatrix <- function(x = matrix()) {
7-
8+
m <- NULL
9+
set <- function(y) {
10+
x <<- y
11+
m <<- NULL
12+
}
13+
get <- function() x
14+
setsolve <- function(solve) m <<- solve
15+
getsolve <- function() m
16+
list(set = set, get = get,
17+
setsolve = setsolve,
18+
getsolve = getsolve)
819
}
920

1021

11-
## Write a short comment describing this function
12-
22+
## Compute the inverse of the special "matrix" returned by makeCacheMatrix
23+
## above. If the inverse has already been calculated (and the matrix has not
24+
## changed), then the cachesolve should retrieve the inverse from the cache.
25+
## Assumption: provided matrix is invertible.
1326
cacheSolve <- function(x, ...) {
1427
## Return a matrix that is the inverse of 'x'
28+
m <- x$getsolve()
29+
if(!is.null(m)) {
30+
message("getting cached data")
31+
return(m)
32+
}
33+
data <- x$get()
34+
m <- solve(data, ...)
35+
x$setsolve(m)
36+
m
1537
}

0 commit comments

Comments
 (0)