'Function Can't Assign a Value Back After Calulcating the Inverse of a Matrix
I am following this assignment link
so I have to create two functions:
- makeCacheMatrix: This function creates a special "matrix" object that can cache its inverse.
- cacheSolve: This function computes the the inverse of the special "matrix" returned by makeCacheMatrix above. If the inverse has already been calculated (and the matrix has not changed), then cacheSolve should retrieve the inverse from the cache.
so that's my solution
makeCacheMatrix <- function(x = matrix()) {
if (ncol(x)==nrow(x) && det(x)!=0) {
m<-NULL
set<-function(y){
x<<-y
m<<-NULL
}
get<-function() x
setinverse <- function(inverse) m <<- inverse
getinverse<-function() m
list(set=set,get=get,setinverse=setinverse,getinverse=getinverse)
}else{
return(message("The matrix is'n invertible."))
}
}
cacheSolve <- function(x, ...) {
m<-x$getinverse()
if (!is.null(m)) {
message("getting cached data")
return(m)
} else{
thematrix <-x$get()
m <- solve(thematrix)
}
return(m)
x$setinverse(m)
}
but when I check if my inverse has been updated using nameofmyspeicalmatrix$getinverse it will give me NULL. can anyone tell me why it won't assign the inverse value?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
