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

Skip to content

Commit 5ebe944

Browse files
author
Oleg Dranitsin
committed
Programming Assignment 2
1 parent 7f657dd commit 5ebe944

File tree

1 file changed

+32
-7
lines changed

1 file changed

+32
-7
lines changed

cachematrix.R

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,40 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
1+
## Cahce inverse of a matrix
2+
## 2 functions below make obect which store matrix
3+
## and calculate its inverse with caching.
34

4-
## Write a short comment describing this function
5+
## makeCacheMatrix funtction creates "matrix"
6+
## object that can cache its inverse.
7+
##
58

69
makeCacheMatrix <- function(x = matrix()) {
7-
10+
inver_m<-NULL
11+
set<-function(y){
12+
x<<-y
13+
inver_m<<-NULL
14+
}
15+
get<-function() x
16+
setInvMat<-function(inverse) inver_m<<- inverse
17+
getInvMat<-function() inver_m
18+
list(set=set, get=get,
19+
setInvMat=setInvMat,
20+
getInvMat=getInvMat)
821
}
922

10-
11-
## Write a short comment describing this function
23+
## Function cahceSolve computes inverse of special "matrix" from
24+
## makeCacheMatrix function indicated above.
25+
## If the inverse has already been calculated (and the matrix has not changed),
26+
## then the cachesolve should retrieve the inverse from the cache.
1227

1328
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
29+
## Return a matrix that is the inverse of 'x'
30+
inver_m<-x$getInvMat()
31+
if(!is.null(inver_m)){
32+
message("getting cached data")
33+
return(inver_m)
34+
}
35+
matrix <- x$get()
36+
inver_m<-solve(matrix, ...)
37+
x$setInvMat(inver_m)
38+
inver_m
39+
1540
}

0 commit comments

Comments
 (0)