Skip to content

Commit f07faae

Browse files
author
DF
committed
Changes in makeCacheMatrix and cacheSolve
Added comments, and creation of makeCacheMatrix body as well as cacheSolve
1 parent 7f657dd commit f07faae

File tree

1 file changed

+27
-4
lines changed

1 file changed

+27
-4
lines changed

cachematrix.R

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,38 @@
11
## Put comments here that give an overall description of what your
22
## functions do
3+
## For costly computation sometimes there's benefit caching the functions.
4+
## The purpose of the functions below is to cache the inverse of a matrix rathen than compute it repeatedly.
35

46
## Write a short comment describing this function
5-
7+
## Creates a special "matrix" object that can cache its inverse
68
makeCacheMatrix <- function(x = matrix()) {
7-
9+
m <- NULL
10+
set <- function(y) {
11+
x <<- y
12+
m <<- NULL
13+
}
14+
get <- function() x
15+
setinverse <- function(inverse) m <<- inverse
16+
getinverse <- function() m
17+
list(set = set, get = get,
18+
setinverse = setinverse,
19+
getinverse = getinverse)
820
}
921

1022

1123
## Write a short comment describing this function
12-
24+
##Computes the inverse of the special "matrix" returned by makeCacheMatrix 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
1327
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
28+
## Return a matrix that is the inverse of 'x'
29+
m <- x$getinverse()
30+
if(!is.null(m)) {
31+
message("getting cached data")
32+
return(m)
33+
}
34+
data <- x$get()
35+
m <- solve(data, ...)
36+
x$setinverse(m)
37+
m
1538
}

0 commit comments

Comments
 (0)