File tree Expand file tree Collapse file tree 1 file changed +52
-6
lines changed Expand file tree Collapse file tree 1 file changed +52
-6
lines changed Original file line number Diff line number Diff line change 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
67makeCacheMatrix <- 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
1338cacheSolve <- 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+ }
You can’t perform that action at this time.
0 commit comments