Skip to content

Commit 51e9a13

Browse files
committed
Changes to be committed:
modified: cachematrix.R
1 parent 7f657dd commit 51e9a13

File tree

1 file changed

+52
-6
lines changed

1 file changed

+52
-6
lines changed

cachematrix.R

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,61 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
1+
## makeCachematrix creates a special list of functions
2+
## that calculate inverse of a matrix and caches it
33

4-
## Write a short comment describing this function
4+
## This function creates a special "matrix" object that can
5+
##cache its inverse.
56

67
makeCacheMatrix <- function(x = matrix()) {
7-
8+
I <- NULL
9+
set <- function(y) {
10+
x <<- y
11+
I <<- NULL
12+
}
13+
get <- function() x
14+
setinv <- function(solve) I <<- solve
15+
getinv <- function () I
16+
list (set = set, get = get,
17+
setinv = setinv,
18+
getinv = getinv)
819
}
920

21+
makeVector <- function(x = numeric()) {
22+
m <- NULL
23+
set <- function(y) {
24+
x <<- y
25+
m <<- NULL
26+
}
27+
get <- function() x
28+
setmean <- function(mean) m <<- mean
29+
getmean <- function() m
30+
list(set = set, get = get,
31+
setmean = setmean,
32+
getmean = getmean)
33+
}
1034

11-
## Write a short comment describing this function
35+
## This function computes the inverse of the special "matrix"
36+
##returned by makeCacheMatrix above.
1237

1338
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
39+
## Return a matrix that is the inverse of 'x'
40+
I <-x$getinv()
41+
if(!is.null(I)) {
42+
message("getting cached data")
43+
return(I)
44+
}
45+
data <-x$get()
46+
I <-solve(data,...)
47+
x$setinv(I)
48+
I
1549
}
50+
51+
cachemean <- function(x, ...) {
52+
m <- x$getmean()
53+
if(!is.null(m)) {
54+
message("getting cached data")
55+
return(m)
56+
}
57+
data <- x$get()
58+
m <- mean(data, ...)
59+
x$setmean(m)
60+
m
61+
}

0 commit comments

Comments
 (0)