forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cachematrix.R - making it available on git
- Loading branch information
Ravi S Mula
committed
Aug 18, 2015
1 parent
412d4dd
commit 6c2607b
Showing
1 changed file
with
36 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,45 @@ | ||
## Put comments here that give an overall description of what your | ||
## functions do | ||
library("MASS") | ||
|
||
## Write a short comment describing this function | ||
## These functions are used to calculates the inverse of a matrix | ||
## stores the result to avoid computations if the result is needed | ||
## again by some other function to make the function more efficient | ||
|
||
makeCacheMatrix <- function(x = matrix()) { | ||
## The function, `makeCacheMatrix` creates a list containing a function to | ||
## 1. set the value of the matrix | ||
## 2. get the value of the matrix | ||
## 3. set the value of the inverse | ||
## 4. get the value of the inverse | ||
|
||
makeCacheMatrix <- function(x = matrix()) { | ||
inv <- NULL | ||
set <- function(y) { | ||
x <<- y | ||
m <<- NULL | ||
} | ||
get <- function() x | ||
setinv <- function(inverse) inv <<- inverse | ||
getinv <- function() inv | ||
list(set = set, get = get, | ||
setinv = setinv, | ||
getinv = getinv) | ||
} | ||
|
||
|
||
## Write a short comment describing this function | ||
## Reads a list created by `makeCacheMatrix` function, | ||
## Checks whether Inverse is calculated or not, | ||
## If calculated , avoids computation and returns the value | ||
## else computes inverse and stores it to avoid recomputing. | ||
|
||
## NOTE: Uses `ginv` function in the package MASS for inverse calculation | ||
|
||
cacheSolve <- function(x, ...) { | ||
## Return a matrix that is the inverse of 'x' | ||
i <- x$getinv() | ||
if(!is.null(i)) { | ||
message("getting cached data...") | ||
return(i) | ||
} | ||
data <- x$get() | ||
i <- ginv(data, ...) | ||
x$setinv(i) | ||
i | ||
} |