Skip to content

Commit 3379308

Browse files
committed
Update cachematrix.R
1 parent 7f657dd commit 3379308

File tree

1 file changed

+46
-6
lines changed

1 file changed

+46
-6
lines changed

cachematrix.R

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,55 @@
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
35

4-
## Write a short comment describing this function
56

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
712

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)
823
}
924

1025

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
1230

1331
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
1541
}
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

Comments
 (0)