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

Skip to content

Commit d470fc2

Browse files
committed
Solution for Assignment 3
https://class.coursera.org/rprog-009/human_grading/view/courses/972583/a ssessments/3/submissions Peer Review
1 parent 7f657dd commit d470fc2

File tree

1 file changed

+40
-11
lines changed

1 file changed

+40
-11
lines changed

cachematrix.R

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,44 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
3-
4-
## Write a short comment describing this function
5-
6-
makeCacheMatrix <- function(x = matrix()) {
1+
## makeCacheMatrix: This function creates a special 'matrix' object
2+
## that can cache its inverse.
3+
## cacheSolve: This function computes the inverse of the special 'matrix'
4+
## returned by makeCacheMatrix above. If the inverse has already been
5+
## calculated (and the matrix has not changed), then the cachesolve
6+
## should retrieve the inverse from the cache.
7+
## Computing the inverse of a square matrix can be done with the solve function in R.
8+
## For example, if X is a square invertible matrix, then solve(X) returns its inverse.
79

10+
## Function builds the psuedo-matrix object, which is really a list of functions to:
11+
## 1. set the matrix value
12+
## 2. get the matrix value
13+
## 3. set the matrix inverse value
14+
## 4. get the matrix inverse value
15+
makeCacheMatrix <- function(m = matrix()) {
16+
m_inverse <- NULL
17+
set <- function(new_m) {
18+
m <<- new_m
19+
m_inverse <<- NULL
20+
}
21+
get <- function() m
22+
setinv <- function(m_inv) m_inverse <<- m_inv
23+
getinv <- function() m_inverse
24+
list(set = set, get = get,
25+
setinv = setinv,
26+
getinv = getinv)
827
}
928

10-
11-
## Write a short comment describing this function
12-
29+
## Calculates the inverse of the special "matrix" created with makeCacheMatrix()
30+
## Tt first checks to see if the matrix inverse has already been calculated.
31+
## If so, it gets the value from the cache and skips the computation.
32+
## Otherwise, it calculates the data and sets the value in the cache
1333
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
15-
}
34+
## Return a matrix that is the inverse of 'x' either via cache or computation
35+
inv <- x$getinv()
36+
if(!is.null(inv)) {
37+
message("getting cached data")
38+
return(inv)
39+
}
40+
data <- x$get()
41+
c_inv <- solve(data, ...)
42+
x$setinv(c_inv)
43+
c_inv
44+
}

0 commit comments

Comments
 (0)