forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcachematrix.R
More file actions
54 lines (41 loc) · 1.68 KB
/
cachematrix.R
File metadata and controls
54 lines (41 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
## makeCacheMatrix and cacheSolve are two functions created to work in
## conjuction to compute the value of inverse matrix, difference is if
## inverse matrix been already created then re computation will not be done
## the result will be fetched from cache instead
## makeCacheMatrix for
## creating a special vector containing a function to
##1. set the value of the vector matrix
##2. get the value of the vector matrix
##3. set the value of the inverse matrix
##4. get the value of the inverse matrix
##
## cacheSolve function computes the inverse matrix using the special vector
## created from previous function, however it first check that if inverse matrix
## already been calculated, if yes then it fetch the values from cache instead of
## recomputation as in usual case
makeCacheMatrix <- function(x = matrix()) {
s <- NULL
set <- function(y) {
x <<- y
s <<- NULL
}
get <- function() x
setsolve <- function(solve) s <<- solve
getsolve <- function() s
list(set = set, get = get,
setsolve = setsolve,
getsolve = getsolve)
}
cacheSolve <- function(x,...) {
s <- x$getsolve() ## Assign Value: special list vector x for object getsolve
if(!is.null(s)) { ## execute code if value is not null
message("getting cached data")
return(s) ## Returning stored value from Cache and exit
}
data <- x$get() ## Assign Value: special list vector x for object get
s <- solve(data,...) ## Calculating inverse of matrix
x$setsolve(s) ## Setting value in list vector x for object setsolve
s ## Returning Newly computed inverse matrix and exit
}
InvExm <- makeCacheMatrix(matrix(1:4,2,2))
cacheSolve(InvExm)