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

Skip to content

Commit 1609204

Browse files
author
olarina
committed
cachematrix.R commit
1 parent f41183e commit 1609204

File tree

1 file changed

+38
-9
lines changed

1 file changed

+38
-9
lines changed

cachematrix.R

Lines changed: 38 additions & 9 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
1+
## Below are two functions that are used to create a special object that stores
2+
## a numeric matrix and caches its inversion
33

4-
## Write a short comment describing this function
4+
## The first function, makeCacheMatrix creates a special "vector"
5+
## which is really a list containing a function to
56

6-
makeCacheMatrix <- function(x = matrix()) {
7-
##test
8-
}
7+
## 1. set the value of the matrix
8+
## 2. get the value of the matrix
9+
## 3. set the value of the inversion
10+
## 4. get the value of the inversion
911

12+
makeCacheMatrix <- function(x = matrix())
13+
{
14+
inv <- NULL
15+
set <- function(y)
16+
{
17+
x <<- y
18+
inv <<- NULL
19+
}
20+
get <- function() x
21+
setinv <- function(i) inv <<- i
22+
getinv <- function() inv
23+
list(set = set, get = get, setinv = setinv, getinv = getinv)
24+
}
1025

11-
## Write a short comment describing this function
26+
## The following function calculates the inversion of the special "vector"
27+
## created with the makeCacheMatrix function. It first checks if the inversion
28+
## has already been calculated. If so, it gets the inversion from the cache and
29+
## skips the computation. Otherwise, it calculates the inversion of the data
30+
## and sets the value of the inversion in the cache via the setinv function.
1231

13-
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
32+
cacheSolve <- function(x, ...)
33+
{
34+
inv <- x$getinv()
35+
if (!is.null(inv))
36+
{
37+
message("getting cached data")
38+
return(inv)
39+
}
40+
data <- x$get()
41+
inv <- solve(data, ...)
42+
x$setinv(inv)
43+
inv
1544
}

0 commit comments

Comments
 (0)