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

Skip to content

Commit 22853d9

Browse files
committed
matrix function
1 parent 7f657dd commit 22853d9

File tree

1 file changed

+35
-2
lines changed

1 file changed

+35
-2
lines changed

cachematrix.R

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,47 @@
22
## functions do
33

44
## Write a short comment describing this function
5+
## The function sets and gets the matrix and its inverse
56

67
makeCacheMatrix <- function(x = matrix()) {
7-
8+
m <- NULL
9+
set <- function(y) {
10+
x <<- y
11+
m <<- NULL
12+
}
13+
get <- function() x
14+
setinverse <- function(solve) m <<- solve
15+
getinverse <- function() m
16+
list(set = set, get = get,
17+
setinverse = setinverse,
18+
getinverse = getinverse)
819
}
920

1021

1122
## Write a short comment describing this function
23+
## the function calculates the inverse of the matrix created above.
24+
## checks to see if inverse is empty and if not uses cached inverse
1225

1326
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
27+
m <- x$getinverse()
28+
if(!is.null(m)) {
29+
message("getting cached data")
30+
return(m)
31+
}
32+
data <- x$get()
33+
m <- solve(data, ...)
34+
x$setinverse(m)
35+
m
1536
}
37+
38+
#to test you do like this:
39+
#>mat1 = matrix(r, nrow=10, ncol=10)
40+
#>x = makeCacheMatrix(mat1)
41+
#>cacheSolve(x)
42+
#and you get the answer very fast! Repeat to see message
43+
#or read in matrix like this:
44+
#temp= read.csv("testfile.csv")
45+
#temp1 <- as.matrix(temp)
46+
#x = makeCacheMatrix(temp1)
47+
#...etc. but don't forget R assumes header and
48+
#it will complain matrix not square if u forget!!!

0 commit comments

Comments
 (0)