I am trying to run this example for the R Programming course on coursEra. However when I try to determine whether or not the matrix is square I get error saying "Error in is.square.matrix(x) : argument x is not a matrix"
My code is below:
##non-square matrix
NCols <- sample(3:6, 1)
NRows <- sample(2:8, 1)
myMat <- matrix(runif(NCols*NRows), ncol=NCols)
is.square.matrix(myMat)
## functions
makeMatrix <- function(x = matrix()) {
m <- NULL
set <- function(y) {
x <<- y
m <<- NULL
}
get <- function() x
setInv <- if (is.square.matrix(x) == TRUE) {
function(solve) m <<- solve
}
{
function(ginv) m <<- ginv
}
getInv <- function() m
list(x, set = set, get = get,
setInv = setInv,
getInv = getInv)
}
cacheMatrix <- function(x, ...) {
m <- x$getInv()
if(!is.null(m)) {
message("getting cached data")
return(m)
}
data <- x$get()
m <- if (is.square.matrix(x) == TRUE) {
solve(data, ...)
}
{
ginv(data, ...)
}
x$setInv(m)
m
}
## run functions for matrix
notSqaure <- makeMatrix(myMat)
cacheMatrix(notSqaure)
##check
ginv(myMat)
Then I get the error:
Error in is.square.matrix(x) : argument x is not a matrix
I am a beginner so not sure how to get the sentInv to recognize and check if the matrix is square or not.
Brian
Aucun commentaire:
Enregistrer un commentaire