Skip to content

Commit 17c0c47

Browse files
author
Samuels
committed
commit rdpeng#1
1 parent 2bcea91 commit 17c0c47

File tree

1 file changed

+26
-7
lines changed

1 file changed

+26
-7
lines changed

cachematrix.R

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,34 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
1+
## Code that sets and gets the values of a matrix
2+
## Then sets and gets the values of the inverse of the matrix
33

4-
## Write a short comment describing this function
5-
6-
makeCacheMatrix <- function(x = matrix()) {
4+
## This function creates a special matrix which is a list containing functions to get the inverse matrix
75

6+
makeCacheMatrix <- function(x = matrix()) { #initialize matrix function
7+
mata <- NULL
8+
set <- function(y) { #set value
9+
x<<-y
10+
mata <<-NULL
11+
}
12+
get <- function() x # get value
13+
setinvmat<- function(inv) mata <<-inv #set value
14+
getinvmat <- function() mata #get value
15+
list(set=set,get=get, setinvmat=setinvmat,getinvmat=getinvmat)
816
}
917

1018

11-
## Write a short comment describing this function
19+
## Function calculates inverse of special matrix: the function checks to see if the inverse was calculated
20+
## if function has been calculated it returns the inverse from cache and skips calculation.
21+
## Otherwise the function calculates the inverse of the matrix
1222

1323
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
24+
mata <- x$getinvmat()
25+
if(!is.null(mata)){
26+
message("getting cached data")
27+
return(mata)
28+
}
29+
data <-x$get()
30+
mata<- solve(data,...)
31+
x$setinvmat(mata)
32+
mata
33+
## Return a matrix that is the inverse of 'x'
1534
}

0 commit comments

Comments
 (0)