Skip to content

Commit 5ad55f2

Browse files
committed
finish
1 parent e4eed41 commit 5ad55f2

File tree

1 file changed

+26
-3
lines changed

1 file changed

+26
-3
lines changed

cachematrix.R

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,38 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
1+
## Function returns the inverse of a square matrix. It does not check that the matrix
2+
## is invertible.
3+
## The function first calls the cache function to see if the result has already been
4+
## computed and either returns the cache or computes the inverse
5+
## The function call is: cacheSolve(makeCacheMatrix(x))
6+
## where x is the invertible matrix.
37

48
## Write a short comment describing this function
59

610
makeCacheMatrix <- function(x = matrix()) {
7-
11+
m <- NULL # initializes m
12+
set <- function(y) {
13+
x <<- y # x survives the end of the function to pass to next
14+
m <<- NULL # resets m to NULL and survives the end of the function
15+
}
16+
get <- function() x # returns just x
17+
setinverse <- function(solve) m <<- solve #the computed value is set into the list here
18+
getinverse <- function() m
19+
list(set = set, get = get,
20+
setinverse = setinverse,
21+
getinverse = getinverse)
822
}
923

1024

1125
## Write a short comment describing this function
1226

1327
cacheSolve <- function(x, ...) {
1428
## Return a matrix that is the inverse of 'x'
29+
m <- x$getinverse() #gets the matrix m from above and tests if it's NULL
30+
if(!is.null(m)) { #if not NULL, returns its value
31+
message("getting cached data")
32+
return(m)
33+
}
34+
data <- x$get() #just gets x from above
35+
m <- solve(data, ...) #compute here
36+
x$setinverse(m) #sets the cache in the function setinverse() above
37+
m
1538
}

0 commit comments

Comments
 (0)