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

Skip to content

Commit 4ebaa9d

Browse files
committed
Coursera programming assignment rdpeng#2
The two functions needed in the assignment.
1 parent 825995c commit 4ebaa9d

File tree

1 file changed

+41
-9
lines changed

1 file changed

+41
-9
lines changed

cachematrix.R

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,47 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
3-
## This is for a test commit to my repo
4-
## Write a short comment describing this function
1+
## "makeCacheMatrix" creates a function that will take x which is assumed to be an invertible matrix and
2+
##will return a list of functions that are able to set and get the matrix and its inverse
3+
##The <<- operator is used to assign a value to a variable in the parent environment
4+
##set nullifies the m which is the inverse matrix
5+
##get returns the matrix
6+
##setinv sets the inverse of the matrix
7+
##getinv returns the inverse of the matrix
8+
##The output is a list containing those functions
59

6-
makeCacheMatrix <- function(x = matrix()) {
10+
makeCacheMatrix<-function (x =as.matrix()){
11+
m <- NULL
712

13+
set <- function (y) {
14+
x <<- y
15+
m <<- NULL
16+
}
17+
get <- function() x
18+
setinv <- function (solved) {
19+
m <<- solved
20+
}
21+
getinv <- function () m
22+
list (set = set,
23+
get = get,
24+
setinv = setinv,
25+
getinv = getinv)
26+
827
}
928

29+
##"cacheSolve" creates a function that will take x, a list of functions that is the output of a matrix
30+
##matrix processed with the "makeCacheMatrix" function and will calculate it's inverse with the solve
31+
##function, unless this has already done.Then, the cached result will be returned
32+
##First the funtion assigns m the value of the inverse matrix, this is either NULL or something computed
33+
##in a previous invoke of the "cacheSolve" function. If m is not NULL then the cached value is returned.
34+
##If m is NULL it calculates the inverse matrix and assigns this to m with the "setinv".
35+
##In the first invoke of "cacheSolve" m is always NULL
1036

11-
## Write a short comment describing this function
12-
13-
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
37+
cacheSolve<-function(x, ...) {
38+
m<-x$getinv()
39+
if(!is.null(m)){
40+
message("getting cached data")
41+
return(m)
42+
}
43+
data <-x$get()
44+
m<- solve(data, ...)
45+
x$setinv(m)
46+
m
1547
}

0 commit comments

Comments
 (0)