8000 completed assignment · tsurudak/ProgrammingAssignment2@8c6fed5 · GitHub
[go: up one dir, main page]

Skip to content
This repository was archived by the owner on Feb 13, 2019. It is now read-only.

Commit 8c6fed5

Browse files
committed
completed assignment
Wrote functions and filled in comments for programming assignment rdpeng#2. Code was tested using matrix(1:4) and performed as expected.
1 parent e4eed41 commit 8c6fed5

File tree

1 file changed

+48
-6
lines changed

1 file changed

+48
-6
lines changed

cachematrix.R

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,57 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
1+
##############################################################################
2+
##
3+
## KT
4+
## Programming assignment 2
5+
## R Programming Course (Coursera, April 2014)
6+
##
7+
## Contains a function that creates a special "matrix" that can cache its inverse
8+
## And a second function that calculates or retrieves (depending on whether it
9+
## exists already) the inverse of said "special matrix and returns that inverse
10+
##
11+
##############################################################################
312

4-
## Write a short comment describing this function
513

6-
makeCacheMatrix <- function(x = matrix()) {
714

15+
## makeCacheMatrix creates a list that contains functions to:
16+
## 1. set the value of a matrix
17+
## 2. get the value of a matrix
18+
## 3. set the value of an invserse of a matrix
19+
## 4. get the value of an invserse of a matrix
20+
21+
makeCacheMatrix <- function(x = matrix()) {
22+
m <- NULL
23+
set <- function(y) {
24+
x <<- y #set matrix equal to matrix value
25+
m <<- NULL #set inverse to NULL
26+
}
27+
get <- function() x ## return matrix
28+
setinverse <- function(inverse) m <<- inverse #set inverse of matrix
29+
getinverse <- function() m #return the inverse of matrix
30+
31+
#return list with functions 1-4 as listed above in large comment
32+
list(set = set,
33+
get = get,
34+
setinverse = setinverse,
35+
getinverse = getinverse)
836
}
937

1038

11-
## Write a short comment describing this function
39+
40+
41+
## cacheSolve returns the inverse of an invertable matrix.
42+
## First, it checks if the inverse of the matrix has already been calculated
43+
## If so, it "gets" this inverse and returns it. Otherwise, it calculates the
44+
## inverse, sets the value of the inverted matrix using the 'setinverse'
45+
## function, and returns the inverse
1246

1347
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
48+
m <- x$getinverse() #check if matrix inverse has already been calculated
49+
if(!is.null(m)) { # if inverse is already calculated
50+
message("fetching cached data")
51+
return(m) # return inverse and exit function
52+
}
53+
data <- x$get() # if matrix inverse hasn't been calculated, get matrix
54+
m <- solve(data, ...) # solve the inverse
55+
x$setinverse(m) # set the inverse
56+
m # return the inverse and exit function
1557
}

0 commit comments

Comments
 (0)
0