|
1 | | -## Put comments here that give an overall description of what your |
2 | | -## functions do |
| 1 | +## Programming Assingment 2 |
| 2 | +## Two functions |
| 3 | +## makeCacheMatrix - creates a list with functions |
| 4 | +## cacheSolve - finds inverse of matrix checking previously cached matrix |
3 | 5 |
|
4 | | -## Write a short comment describing this function |
5 | 6 |
|
6 | | -makeCacheMatrix <- function(x = matrix()) { |
| 7 | +## creates a list with functions |
| 8 | +## set the value of matrix |
| 9 | +## get the value of matrix |
| 10 | +## set the value of inverse |
| 11 | +## get the value of inverse |
7 | 12 |
|
| 13 | +makeCacheMatrix <- function(x = matrix()) { |
| 14 | + i<-NULL |
| 15 | + set<-function(y){ |
| 16 | + x<<-y |
| 17 | + i<<-NULL |
| 18 | + } |
| 19 | + get<-function() x |
| 20 | + setmatrix<-function(solve) i<<- solve |
| 21 | + getmatrix<-function() i |
| 22 | + list(set=set, get=get, setmatrix=setmatrix,getmatrix=getmatrix) |
8 | 23 | } |
9 | 24 |
|
10 | 25 |
|
11 | | -## Write a short comment describing this function |
| 26 | +## Calculates mean of matrix |
| 27 | +## first checks to see if mean has already been calculated |
| 28 | +## if so skips computation |
| 29 | +## else calculates the mean |
12 | 30 |
|
13 | 31 | cacheSolve <- function(x, ...) { |
14 | | - ## Return a matrix that is the inverse of 'x' |
| 32 | + i<-x$getmatrix() |
| 33 | + if(!is.null(i)){ |
| 34 | + message("getting cached data") |
| 35 | + return(i) |
| 36 | + } |
| 37 | + matrix<-x$get() |
| 38 | + i<-solve(matrix, ...) |
| 39 | + x$setmatrix(i) |
| 40 | + i |
15 | 41 | } |
| 42 | + |
| 43 | +## --------------------------Test run commands---------------- |
| 44 | +## > test<- rbind (c(1.0, -0.25), c(-0.25,1)) |
| 45 | +## > z <- makeCacheMatrix(test) |
| 46 | +## > cacheSolve(z) |
| 47 | +## [,1] [,2] |
| 48 | +## [1,] 1.0666667 0.2666667 |
| 49 | +## [2,] 0.2666667 1.0666667 |
| 50 | +## -----------if you again run then it says getting cached data------- |
| 51 | +## > cacheSolve(z) |
| 52 | +## getting cached data |
| 53 | +## [,1] [,2] |
| 54 | +## [1,] 1.0666667 0.2666667 |
| 55 | +## [2,] 0.2666667 1.0666667 |
0 commit comments