-
Notifications
You must be signed in to change notification settings - Fork 144k
Expand file tree
/
Copy pathcachematrix.R
More file actions
31 lines (27 loc) · 925 Bytes
/
cachematrix.R
File metadata and controls
31 lines (27 loc) · 925 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
## This function creates a special matrix object that can store a matrix ans its cached inverse
makeCacheMatrix <- function(x = matrix()) {
m <- NULL
set <- function(y){
x <<- y
m <<- NULL #Reset the inverse when the matrix changes
}
get <- function() x
setsolve <- function(solve) m <<- solve
getsolve <- function() m
list(set =set, get=get,
setsolve=setsolve,
getsolve=getsolve)
}
## Computes the inverse of the special matrix object
cacheSolve <- function(x, ...) { #first checks if it has been already calculated
m <- x$getsolve()
if(!is.null(m)) {
message("getting cached data") #if it has been calculated return the cached value
return(m)
}
data <- x$get() #if it has not been calculated it makes the calculation
m <-solve (data, ...)
x$setsolve(m)
m
}
## Return a matrix that is the inverse of 'x'