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

Skip to content

Commit e4bd6d5

Browse files
committed
Created functions
Created functions makeCacheMatrix and cacheSolve.
1 parent 7f657dd commit e4bd6d5

File tree

1 file changed

+34
-4
lines changed

1 file changed

+34
-4
lines changed

cachematrix.R

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,45 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
1+
## The following functions are used to compute the inverse matrix of an input matrix and cache its result.
2+
## If the inverse matrix of the input matrix has already been computed, a cached result is returned.
3+
## Otherwise, the inverse matrix is computed.
4+
## Test, create matrix: mat <- matrix(c(51, 60, 7, 8), nrow=2, ncol=2)
5+
##
36

4-
## Write a short comment describing this function
7+
8+
## makeCacheMatrix: Creates a a special object "matrix" that stores a matrix, computes its inverse using the solve function
9+
## and caches the result.
10+
##Test, amekCacheMatrix: a<-makeCacheMatrix(mat)
511

612
makeCacheMatrix <- function(x = matrix()) {
13+
14+
m <- NULL
15+
set <- function(y) {
16+
x <<- y
17+
m <<- NULL
18+
}
19+
get <- function() x
20+
setInvMatrix <- function(solve) m <<- solve
21+
getInvMatrix <- function() m
22+
list(set = set, get = get,
23+
setInvMatrix = setInvMatrix,
24+
getInvMatrix = getInvMatrix)
725

826
}
927

1028

11-
## Write a short comment describing this function
29+
## cacheSolve: Calculates the inverse matrix using the solve function with the matrix object
30+
## created in the makeCacheMatrix function.
31+
##Test, get cachec value or compute: cacheSolve(a)
1232

1333
cacheSolve <- function(x, ...) {
1434
## Return a matrix that is the inverse of 'x'
35+
36+
m <- x$getInvMatrix()
37+
if(!is.null(m)) {
38+
message("getting cached data")
39+
return(m)
40+
}
41+
data <- x$get()
42+
m <- solve(data, ...)
43+
x$setInvMatrix(m)
44+
m
1545
}

0 commit comments

Comments
 (0)