forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.R
More file actions
72 lines (66 loc) · 1.96 KB
/
code.R
File metadata and controls
72 lines (66 loc) · 1.96 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
R version 3.3.2 (2016-10-31) -- "Sincere Pumpkin Patch"
Copyright (C) 2016 The R Foundation for Statistical Computing
Platform: x86_64-w64-mingw32/x64 (64-bit)
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.
Natural language support but running in an English locale
R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.
Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.
> makeMatrix <- function(x = matrix()) {
+ MATINVE <- NULL
+ set <- function(y) {
+ x <<- y
+ MATINVE <<- NULL
+ }
+ get <- function() x
+ setinverse <- function(inverse) MATINVE <<- inverse
+ getinverse <- function() MATINVE
+ list(set=set, get=get, setinverse=setinverse, getinverse=getinverse)
+ }
>
>
>
>
>
> cachematrix <- function(x, ...) {
+ MATINVE <- x$getinverse()
+ if(!is.null(MATINVE )) {
+ message("getting cached matrix data.")
+ return(MATINVE )
+ }
+ data <- x$get()
+ MATINVE<- solve(data)
+ x$setinverse(MATINVE)
+ MATINVE
+ }
> x<-rbind(c(1,2,3),c(0,1,5),c(5,6,0)).
Error: unexpected symbol in "x<-rbind(c(1,2,3),c(0,1,5),c(5,6,0))."
> x<-rbind(c(1,2,3),c(0,1,5),c(5,6,0))
> x
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 0 1 5
[3,] 5 6 0
> m=makeMatrix(x)
> m$get()
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 0 1 5
[3,] 5 6 0
> cachematrix(m)
[,1] [,2] [,3]
[1,] -6 3.6 1.4
[2,] 5 -3.0 -1.0
[3,] -1 0.8 0.2
> cachematrix(m)
getting cached matrix data.
[,1] [,2] [,3]
[1,] -6 3.6 1.4
[2,] 5 -3.0 -1.0
[3,] -1 0.8 0.2
>