diff --git a/ProgrammingAssignment2 b/ProgrammingAssignment2 new file mode 160000 index 00000000000..e4eed4153bd --- /dev/null +++ b/ProgrammingAssignment2 @@ -0,0 +1 @@ +Subproject commit e4eed4153bd237a2dd4532fc3f40ce0e22fd0cb7 diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..64dc853e540 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,30 @@ -## Put comments here that give an overall description of what your -## functions do +#makeCacheMatrix: This function creates a special "matrix" object that can cache its inverse. -## Write a short comment describing this function makeCacheMatrix <- function(x = matrix()) { - + inverse<-NULL + set<-function(y){ + x<<-y + inverse<-NULL + } + get<-function()x + + setinverse<-function(inverse)inverse<<-solve(x) + + getinverse<-function()inverse + list(set=set, get=get,setinverse=setinverse, getinverse=getinverse) } - -## Write a short comment describing this function +#cacheSolve: 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 the cachesolve should retrieve the inverse from the cache. cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' + inverse<-x$getinverse() + if(!is.null(inverse)){ + message("getting cached data") + return(inverse) + } + data<-as.matrix(x$get()) + inverse<-solve(data) + x$setinverse(inverse) + inverse }