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
74 lines (65 loc) · 1.69 KB
/
cachematrix.R
File metadata and controls
74 lines (65 loc) · 1.69 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
## This file contains two functions whose purpose is computes the inverse of a
## matrix on the first run and on subsequent runs returns the cached inverse
##
## Example Usage :
##> c <- makeCacheMatrix()
##> c$set(matrix)
##> cacheSolve(c)
## [,1] [,2]
## [1,] 1.0666667 0.2666667
## [2,] 0.2666667 1.0666667
##> cacheSolve(c)
## getting cached data
## [,1] [,2]
## [1,] 1.0666667 0.2666667
## [2,] 0.2666667 1.0666667
## Function makeCacheMatrix
## Written : 27.Jul.2014
## Description : This will functions keeps two internal variable : i and x
## i -> saved inverse of the matrix
## x -> the actual matrix
## Usage :
##> c <- makeCacheMatrix()
##> c$set(matrix)
##> c$get(matrix)
##> c$setinverse(solve(matrix))
##> c$getinverse()
makeCacheMatrix <- function(x = matrix())
{
i <- NULL
set <- function(y)
{
x <<- y
i <<- NULL
}
get <- function() x
setinverse <- function(inverse) i <<- inverse
getinverse <- function() i
list(set = set, get = get,
setinverse = setinverse,
getinverse = getinverse)
}
## Function cacheSolve
## Written : 27.Jul.2014
## Input: 'x' which is of type makeCacheMatrix.
## Output: Returns the inverse of the matrix saved in 'x'.
## Description : Will compute the inverse of the matrix in 'x' and
## save it into cache of 'x' on the first run. On subsequent
## run, will just get the cached version saved into 'x'.
## Usage :
##> c <- makeCacheMatrix()
##> c$set(matrix)
##> cacheSolve(c)
cacheSolve <- function(x, ...)
{
inverse <- x$getinverse()
if(!is.null(inverse))
{
message("getting cached data")
return(inverse)
}
data <- x$get()
inverse <- solve(data, ...)
x$setinverse(inverse)
inverse
}