|
1 |
| -## Put comments here that give an overall description of what your |
2 |
| -## functions do |
| 1 | +## |
| 2 | +## R Programming, Assignment 2 |
| 3 | +## Uses lexical scoping to create and cache the inverted matrix of a given matrix. |
| 4 | +## |
3 | 5 |
|
4 |
| -## Write a short comment describing this function |
| 6 | +# these lines are just for testing... |
| 7 | +#rm(list=ls()) |
| 8 | +#setwd("~/Personal/Coursera/RProgramming/Labs/ProgrammingAssignment2"); |
| 9 | +#getwd() |
5 | 10 |
|
6 |
| -makeCacheMatrix <- function(x = matrix()) { |
7 | 11 |
|
| 12 | +## makeCacheMatrix(x) |
| 13 | +## x is a matrix |
| 14 | +## returns a list of functions to manipulate x: |
| 15 | +## set(), get(), setinverse(), and getinverse() |
| 16 | +makeCacheMatrix <- function(x = matrix()) { |
| 17 | + invm <- NULL |
| 18 | + set <- function(y) { |
| 19 | + x <<- y |
| 20 | + invm <<- NULL |
| 21 | + } |
| 22 | + get <- function() x |
| 23 | + setinverse <- function(m) invm <<- m |
| 24 | + getinverse <- function() invm |
| 25 | + list(set = set, get = get, |
| 26 | + setinverse = setinverse, |
| 27 | + getinverse = getinverse) |
8 | 28 | }
|
9 | 29 |
|
10 | 30 |
|
11 |
| -## Write a short comment describing this function |
12 |
| - |
| 31 | +## Return a matrix that is the inverse of 'x' (created by makeCacheMatrix) |
| 32 | +## and caches it in the object x |
13 | 33 | cacheSolve <- function(x, ...) {
|
14 |
| - ## Return a matrix that is the inverse of 'x' |
| 34 | + invm <- x$getinverse() |
| 35 | + |
| 36 | + if(!is.null(invm)) { |
| 37 | + message("getting cached data") |
| 38 | + return(invm) |
| 39 | + } |
| 40 | + |
| 41 | + data <- x$get() |
| 42 | + invm <- solve(data, ...) |
| 43 | + x$setinverse(invm) |
| 44 | + invm |
15 | 45 | }
|
| 46 | + |
0 commit comments