From 732bb837a946718e7225bd7b5ebd4ef7ba6ad78c Mon Sep 17 00:00:00 2001 From: searoso Date: Sun, 25 Jan 2015 05:29:08 +0300 Subject: [PATCH] Update cachematrix.R --- cachematrix.R | 41 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 36 insertions(+), 5 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..6a630bca981 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,46 @@ -## Put comments here that give an overall description of what your -## functions do +## R Programming Programming Assignment 2 -## Write a short comment describing this function + +## name: makeCacheMatrix +## purpose: This function creates a special "matrix" object that can cache its inverse. makeCacheMatrix <- function(x = matrix()) { + matrix <- NULL + start <- function(t) { + x <<- t + matrix <<- NULL + } + fin <- function() x + # sets inverse of matrix + inverseset <- function(solve) matrix <<- solve + # inversing of matrix + inverseget <- function() matrix + # returns several functions + list(set = start, get = fin, + setinverse = inverseset, + getinverse = inverseget) + } -## Write a short comment describing this function +## name: cacheSolve +## purpose: The function do the inverse of the special "matrix" which is returned by makeCacheMatrix we have above. +## In case of inverse has already been calculated (and the matrix is the same), then the cachesolve +## return the inverse from the cache. cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' + # Doing a matrix that is the inverse of x + matrix <- x$getinverse() + # Check if value is cached + if(!is.null(matrix)) { + message("wait for cached data") + return(matrix) + } + data <- x$get() + # do the inverse + matrix <- solve(data, ...) + # setting cache value + x$setinverse(matrix) + matrix }