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

Skip to content

Commit c461d0c

Browse files
committed
completing assignment (coding segment)
1 parent 7f657dd commit c461d0c

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.Rproj.user
2+
.Rhistory
3+
.RData
4+
ProgrammingAssignment2.Rproj

cachematrix.R

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,43 @@
11
## Put comments here that give an overall description of what your
22
## functions do
33

4-
## Write a short comment describing this function
4+
## Returns an object ( a list of functions ) which will allow
5+
## us to operate on a matrix in a number of functional ways
6+
## including storing a cached matrix inverse for the given matrix
57

68
makeCacheMatrix <- function(x = matrix()) {
7-
9+
x_inverse = NULL
10+
list(
11+
set = function(y) {
12+
x <<- y
13+
x_inverse <<- NULL
14+
},
15+
get = function(){
16+
x
17+
},
18+
setinverse = function(y_inverse){
19+
x_inverse <<- y_inverse
20+
},
21+
getinverse = function(){
22+
x_inverse
23+
}
24+
)
825
}
926

1027

11-
## Write a short comment describing this function
28+
## for a given cacheMatrix (as returned from makeCacheMatrix)
29+
## either calculate or return a cache inverse. A cleaner
30+
## method may just use a memoise-pattern in the makeCacheMatrix
1231

1332
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
33+
## Return a matrix that is the inverse of 'x'
34+
x_inverse = x$getinverse()
35+
if(!is.null(x_inverse)) {
36+
message("getting cached matrix inverse")
37+
return(x_inverse)
38+
}
39+
x_matrix = x$get()
40+
x_inverse = solve(x_matrix, ...)
41+
x$setinverse(x_inverse)
42+
x_inverse
1543
}

0 commit comments

Comments
 (0)