Skip to content

Commit

Permalink
programming assignment2
Browse files Browse the repository at this point in the history
  • Loading branch information
mksarnala committed May 15, 2017
1 parent 7f657dd commit 212ce01
Showing 1 changed file with 29 additions and 12 deletions.
41 changes: 29 additions & 12 deletions cachematrix.R
Original file line number Diff line number Diff line change
@@ -1,15 +1,32 @@
## Put comments here that give an overall description of what your
## functions do

## Write a short comment describing this 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 inverse of the matrix
#4. get the value of inverse of the matrix
makeCacheMatrix <- function(x = matrix()) {

inv <- NULL
set <- function(y) {
x <<- y
inv <<- NULL
}
get <- function() x
setinverse <- function(inverse) inv <<- inverse
getinverse <- function() inv
list(set=set, get=get, setinverse=setinverse, getinverse=getinverse)
}


## Write a short comment describing this function

# The following function returns the inverse of the matrix. It first checks if
# the inverse has already been computed. If so, it gets the result and skips the
# computation. If not, it computes the inverse, sets the value in the cache via
# setinverse function.
# This function assumes that the matrix is always invertible.
cacheSolve <- function(x, ...) {
## Return a matrix that is the inverse of 'x'
}
inv <- x$getinverse()
if(!is.null(inv)) {
message("getting cached data.")
return(inv)
}
data <- x$get()
inv <- solve(data)
x$setinverse(inv)
inv
}

1 comment on commit 212ce01

@mksarnala
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file showing an extra flower braces in line 15 but my actual file didn't have it. here is the original code:

makeCacheMatrix creates a list containing a function to

rdpeng#1. set the value of the matrix
rdpeng#2. get the value of the matrix
rdpeng#3. set the value of inverse of the matrix
rdpeng#4. get the value of inverse of the matrix
makeCacheMatrix <- function(x = matrix()) {
inv <- NULL
set <- function(y) {
x <<- y
inv <<- NULL
}
get <- function() x
setinverse <- function(inverse) inv <<- inverse
getinverse <- function() inv
list(set=set, get=get, setinverse=setinverse, getinverse=getinverse)
}

The following function returns the inverse of the matrix. It first checks if

the inverse has already been computed. If so, it gets the result and skips the

computation. If not, it computes the inverse, sets the value in the cache via

setinverse function.

This function assumes that the matrix is always invertible.

cacheSolve <- function(x, ...) {

inv <- x$getinverse()
if(!is.null(inv)) {
    message("getting cached data.")
    return(inv)
}
data <- x$get()
inv <- solve(data)
x$setinverse(inv)
inv

}

Please sign in to comment.