Skip to content

Commit a3ffe84

Browse files
first commit
1 parent 7f657dd commit a3ffe84

File tree

2 files changed

+66
-5
lines changed

2 files changed

+66
-5
lines changed

Diff for: cachematrix.R

+29-5
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,39 @@
11
## Put comments here that give an overall description of what your
22
## functions do
3-
3+
# 1. getmatrix - gets the matrix
4+
# 2. setmatrix - sets the matrix
5+
# 3. setinverse - sets the inverse
6+
# 4. getinverse - gets the inverse
47
## Write a short comment describing this function
8+
## makeCacheMatrix - returns a list of functions 1,2,3,4
9+
510

611
makeCacheMatrix <- function(x = matrix()) {
712

13+
inv = NULL
14+
setmatrix = function(y) {
15+
x <<- y
16+
inv <<- NULL
17+
}
18+
getmatrix = function() x
19+
setinverse = function(inverse) inv <<- inverse
20+
getinverse = function() inv
21+
list(setmatrix=setmatrix, getmatrix=getmatrix, setinverse=setinverse, getinverse=getinverse)
822
}
923

10-
11-
## Write a short comment describing this function
12-
1324
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
25+
26+
inv = x$getinverse()
27+
28+
if (!is.null(inv)){
29+
message("getting cached data")
30+
return(inv)
31+
}
32+
33+
mat.data = x$get()
34+
inv = solve(mat.data, ...)
35+
36+
x$setinverse(inv)
37+
38+
return(inv)
1539
}

Diff for: cachematrix.R~

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
## Put comments here that give an overall description of what your
2+
## functions do
3+
# 1. getmatrix - gets the matrix
4+
# 2. setmatrix - sets the matrix
5+
# 3. setinverse - sets the inverse
6+
# 4. getinverse - gets the inverse
7+
## Write a short comment describing this function
8+
9+
makeCacheMatrix <- function(x = matrix()) {
10+
11+
inv = NULL
12+
setmatrix = function(y) {
13+
x <<- y
14+
inv <<- NULL
15+
}
16+
getmatrix = function() x
17+
setinverse = function(inverse) inv <<- inverse
18+
getinverse = function() inv
19+
list(setmatrix=setmatrix, getmatrix=getmatrix, setinverse=setinverse, getinverse=getinverse)
20+
}
21+
22+
cacheSolve <- function(x, ...) {
23+
24+
inv = x$getinverse()
25+
26+
if (!is.null(inv)){
27+
message("getting cached data")
28+
return(inv)
29+
}
30+
31+
mat.data = x$get()
32+
inv = solve(mat.data, ...)
33+
34+
x$setinverse(inv)
35+
36+
return(inv)
37+
}

0 commit comments

Comments
 (0)