The following code demonstrates how to use the makeCacheMatrixMean.R script.
Lines starting with # are simple comments, lines starting with #> are things printed in the output.
source("makeCacheMatrixMean.R")
a <- makeCacheMatrix( matrix(c(1,2,12,13), nrow = 2, ncol = 2) );
summary(a);
#> Length Class Mode
#> setMatrix 1 -none- function
#> getMatrix 1 -none- function
#> cacheInverse 1 -none- function
#> getInverse 1 -none- function
a$getMatrix(); #> [,1] [,2] #> [1,] 1 12 #> [2,] 2 13
cacheSolve(a) #> [,1] [,2] #> [1,] -1.1818182 1.09090909 #> [2,] 0.1818182 -0.09090909
cacheSolve(a) #> getting cached data #> [,1] [,2] #> [1,] -1.1818182 1.09090909 #> [2,] 0.1818182 -0.09090909 Alternatively, the matrix can be created after calling a makeCacheMatrix without arguments.
source("path/to/file/assessment3.R")
a <- makeCacheMatrix();
summary(a);
#> Length Class Mode
#> setMatrix 1 -none- function
#> getMatrix 1 -none- function
#> cacheInverse 1 -none- function
#> getInverse 1 -none- function
#if the matrix is not square,it will prompt like this :
a<-makeCacheMatrix() summary(a) Length Class Mode
setMatrix 1 -none- function getMatrix 1 -none- function cacheInverse 1 -none- function getInverse 1 -none- function a$setMatrix(matrix(c(1,2,3,4,5,6),nrow=3,ncol=2)) a$getMatrix() [,1] [,2] [1,] 1 4 [2,] 2 5 [3,] 3 6 cacheSolve(a) #"Error in solve.default(data) : 'a' (3 x 2) must be square"
a$setMatrix( matrix(c(1,2,12,13), nrow = 2, ncol = 2) ); a$getMatrix(); #> [,1] [,2] #> [1,] 1 12 #> [2,] 2 13
cacheSolve(a) #> [,1] [,2] #> [1,] -1.1818182 1.09090909 #> [2,] 0.1818182 -0.09090909
cacheSolve(a) #> getting cached data #> [,1] [,2] #> [1,] -1.1818182 1.09090909 #> [2,] 0.1818182 -0.09090909