Skip to content

Commit ad84df1

Browse files
committed
Assignment
1 parent 7f657dd commit ad84df1

File tree

1 file changed

+64
-15
lines changed

1 file changed

+64
-15
lines changed

cachematrix.R

Lines changed: 64 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,64 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
3-
4-
## Write a short comment describing this function
5-
6-
makeCacheMatrix <- function(x = matrix()) {
7-
8-
}
9-
10-
11-
## Write a short comment describing this function
12-
13-
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
15-
}
1+
## Put comments here that give an overall description of what your
2+
## functions do
3+
4+
#
5+
6+
## Write a short comment describing this function
7+
8+
makeCacheMatrix <- function(x = matrix()) {
9+
10+
#Description: This function creates a special "matrix" object
11+
#that can cache its inverse
12+
13+
#Input: x as a square, invertible matrix (Input from cacheSolve)
14+
#1. set the value of the matrix
15+
#2. get the value of the matrix
16+
#3. set the value of the inverse
17+
#4. get the value of the inverse
18+
#Output:
19+
20+
inverse_i <- NULL
21+
22+
set <- function(y) {
23+
x <<- y
24+
inverse_i <<- NULL
25+
}
26+
27+
get <- function() x
28+
setinverse <- function(inverse) inverse_i <<- inverse
29+
getinverse <- function() inverse_i
30+
31+
list(set = set, get = get, setinverse = setinverse, getinverse = getinverse)
32+
33+
}
34+
35+
36+
37+
38+
39+
## Write a short comment describing this function
40+
41+
cacheSolve <- function(x, ...) {
42+
## Return a matrix that is the inverse of 'x'
43+
44+
#Description: Return a matrix that is the inverse of 'x'
45+
#Input: x = output from function makeCacheMatrix()
46+
#Output: Inverse of x => input for makeCacheMatrix()
47+
48+
#Check if the inverse of x has already been cached/ calculated
49+
inverse_i=x$getinverse()
50+
51+
if(!is.null(inverse_i)) {
52+
message("getting cached data...")
53+
return(inverse_i)
54+
}
55+
56+
#In case the inverse of x has not been calculated/ cached yet, calculate the inverse
57+
inverse_data.data = x$get()
58+
inverse_i=solve(inverse_data.data, ...)
59+
60+
#Set and return the inverse
61+
x$setinverse(inverse_i)
62+
63+
return (inverse_i)
64+
}

0 commit comments

Comments
 (0)