I am trying to inverse a matrix using two sets of functions :- Code is :- (comments describe the process)
## This function creates a special "matrix" object that can cache its inverse.
makeCacheMatrix <- function(x = matrix()) {
m <- NULL
set <- function(y){
x <<- y
m <<- NULL
}
get <- function() x
##solve function returns the inverse.
setInverse <- function(solve) m <<- solve
getInverse <- function() m
list(set = set, get = get, setInverse = setInverse, getInverse)}
This function computes 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.
cacheSolve <- function(x, ...) {
m <- x$getInverse()
if(!is.null(m)){
message("HEY HEY")
return(m)
}
data <- x$get()
m <- solve(data, ...)
x$setInverse(m)
m }
i am trying to run this over matrix a which is defines as a<-diag(2,2)
Then, 1) cm<-makeCachedMatrix
and finally 2) cacheSolve(cm)
.
The number1 step executes however for number 2 there is an error which says :-
Error in cacheSolve(CachedMatrix) : attempt to apply non-function
Any idea on how to fix this? as it does not make sense to me.