Skip to content

Commit 38b244b

Browse files
committed
Finished Programming Assignment 2 - 26-04-2014; 14:46
1 parent 7f657dd commit 38b244b

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

MeanVector.R

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
makeVector <- function(x = numeric()) {
2+
m <- NULL
3+
set <- function(y) {
4+
x <<- y
5+
m <<- NULL
6+
}
7+
get <- function() x
8+
setmean <- function(mean) m <<- mean
9+
getmean <- function() m
10+
list(set = set, get = get,
11+
setmean = setmean,
12+
getmean = getmean)
13+
}
14+
15+
16+
cachemean <- function(x, ...) {
17+
m <- x$getmean()
18+
if(!is.null(m)) {
19+
message("getting cached data")
20+
return(m)
21+
}
22+
data <- x$get()
23+
m <- mean(data, ...)
24+
x$setmean(m)
25+
m
26+
}

cachematrix.R

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,38 @@
44
## Write a short comment describing this function
55

66
makeCacheMatrix <- function(x = matrix()) {
7-
7+
cachedmatrix <- NULL
8+
set <- function(y) {
9+
x <<- y
10+
cachedmatrix <<- NULL
11+
}
12+
get <- function() {
13+
x
14+
}
15+
setinverse <- function(solve) {
16+
cachedmatrix <<- solve
17+
}
18+
getinverse <- function() {
19+
cachedmatrix
20+
}
21+
list(set = set, get = get,
22+
setinverse = setinverse,
23+
getinverse = getinverse)
824
}
925

1026

1127
## Write a short comment describing this function
1228

1329
cacheSolve <- function(x, ...) {
1430
## Return a matrix that is the inverse of 'x'
31+
32+
cachedmatrix <- x$getinverse()
33+
if(!is.null(cachedmatrix)) {
34+
message("getting cached data")
35+
return(cachedmatrix)
36+
}
37+
data <- x$get()
38+
cachedmatrix <- solve(data)
39+
x$setinverse(cachedmatrix)
40+
cachedmatrix
1541
}

0 commit comments

Comments
 (0)