diff --git a/.RData b/.RData new file mode 100644 index 00000000000..024a28eb4d1 Binary files /dev/null and b/.RData differ diff --git a/.Rhistory b/.Rhistory new file mode 100644 index 00000000000..3ced35ef1ae --- /dev/null +++ b/.Rhistory @@ -0,0 +1,46 @@ +source('~/dev/r/ProgrammingAssignment2/cachematrix.R') +x <-matrix(rnorm(20), rif(10)) +x <-matrix(rnorm(20), rnorm(10)) +x <-matrix(rnorm(20), rnorm(20)) +x <-matrix(rnorm(20), rnorm(20)) +?matrix +mdat <- matrix(c(1,2,3, 11,12,13), nrow = 2, ncol = 3, byrow = TRUE, +dimnames = list(c("row1", "row2"), +c("C.1", "C.2", "C.3")) +mdat +mdat <- matrix(c(1,2,3, 11,12,13), nrow = 2, ncol = 3, byrow = TRUE, +dimnames = list(c("row1", "row2"), +c("C.1", "C.2", "C.3"))) +mdat +makeCacheMatrix(mdat) +cacheSolve(m) +cacheSolve(mdat) +d = makeCacheMatrix(mdat) +d +d$get() +cacheSolve(d) +mm <- matrix(c(1,2,3), c(4,5,6), c(7,8,9)) +mm <- matrix(c[1,2,3], c[4,5,6], c[7,8,9]) +b = matrix( c(1,2,3,4), nrow=2, ncol=2) +c = makeInverse(b) +c = makeCacheMatrix(b) +b$get +c$get +c$get() +d = cacheSolve(c) +debugSource('~/dev/r/ProgrammingAssignment2/cachematrix.R') +} +d = cacheSolve(c) +c$getInverse() +c$getInverse +debugSource('~/dev/r/ProgrammingAssignment2/cachematrix.R') +c$getInverse() +d = cacheSolve(c) +d +c$getInverse() +d = cacheSolve(c) +c$getInverse() +c$getInverse +d = cacheSolve(c) +debugSource('~/dev/r/ProgrammingAssignment2/cachematrix.R') +debugSource('~/dev/r/ProgrammingAssignment2/cachematrix.R') diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..d81b17a3523 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,36 @@ -## Put comments here that give an overall description of what your -## functions do +## Cache the inverse of a matrix. -## Write a short comment describing this function +## Create a special matrix which can store its inverse. makeCacheMatrix <- function(x = matrix()) { - + m <- NULL + set <- function(y){ + x <<- y + m <<- NULL + } + get <- function() x + setInverse <- function(solve) m <<- solve + getInverse <- function() m + list(set = set, get = get, + setInverse = setInverse, + getInverse = getInverse) } -## Write a short comment describing this function +## Before compute the inverse of matrix x, see if x has it cached , and return that if do. cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' + ## Return a matrix that is the inverse of 'x' + m <- x$getInverse() + if (!is.null(m)) { + message("getting cached data") + return (m) + } + data <- x$get() + m <- solve(data, ...) + x$setInverse(m) + m } + + +