Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 95a949d

Browse files
committed
commit rdpeng#1
1 parent 7f657dd commit 95a949d

File tree

3 files changed

+55
-4
lines changed

3 files changed

+55
-4
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.Rproj.user
2+
.Rhistory
3+
.RData
4+
.Ruserdata

ProgrammingAssignment2.Rproj

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Version: 1.0
2+
3+
RestoreWorkspace: Default
4+
SaveWorkspace: Default
5+
AlwaysSaveHistory: Default
6+
7+
EnableCodeIndexing: Yes
8+
UseSpacesForTab: Yes
9+
NumSpacesForTab: 2
10+
Encoding: UTF-8
11+
12+
RnwWeave: Sweave
13+
LaTeX: pdfLaTeX

cachematrix.R

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,48 @@
22
## functions do
33

44
## Write a short comment describing this function
5-
5+
## makeCacheMatrix will return a list containing 4 functions, set(), get(), setinverse() and getinverse()
6+
## in function space, it holds matrix x and inverse inv_x
67
makeCacheMatrix <- function(x = matrix()) {
7-
8+
inv_x <- NULL
9+
set <- function(y) {
10+
x <<- y
11+
}
12+
get <- function() x
13+
setinverse <- function(y) inv_x <<- y
14+
getinverse <- function() inv_x
15+
list(set = set, get = get,
16+
setinverse = setinverse,
17+
getinverse = getinverse)
818
}
919

1020

1121
## Write a short comment describing this function
12-
22+
## cacheSolve will return the inverse matrix inv_x and alse set the inv_x held in makeCacheMatrix
1323
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
24+
## Return a matrix that is the inverse of 'x'
25+
inv_m <- x$getinverse()
26+
if(!is.null(inv_m)) {
27+
message("getting cached data")
28+
return(inv_m)
29+
}
30+
31+
m <- x$get()
32+
inv_m <- solve(m)
33+
x$setinverse(inv_m)
34+
inv_m
1535
}
36+
37+
###testing
38+
a <- matrix(c(1,3,2,5,4,3,3,4,5),nrow=3,ncol=3)
39+
x <- makeCacheMatrix(a)
40+
x$get()
41+
x$getinverse()
42+
cacheSolve(x)
43+
b1 <- x$get()
44+
b2 <- x$getinverse()
45+
b1
46+
b2
47+
b1 %*% b2
48+
b2 %*% b1
49+

0 commit comments

Comments
 (0)