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

Skip to content

Commit 075b63a

Browse files
committed
R Programming Assignment 2 completed
1 parent 7f657dd commit 075b63a

File tree

2 files changed

+63
-7
lines changed

2 files changed

+63
-7
lines changed

cachematrix.R

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,46 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
1+
##
2+
## R Programming, Assignment 2
3+
## Uses lexical scoping to create and cache the inverted matrix of a given matrix.
4+
##
35

4-
## Write a short comment describing this function
6+
# these lines are just for testing...
7+
#rm(list=ls())
8+
#setwd("~/Personal/Coursera/RProgramming/Labs/ProgrammingAssignment2");
9+
#getwd()
510

6-
makeCacheMatrix <- function(x = matrix()) {
711

12+
## makeCacheMatrix(x)
13+
## x is a matrix
14+
## returns a list of functions to manipulate x:
15+
## set(), get(), setinverse(), and getinverse()
16+
makeCacheMatrix <- function(x = matrix()) {
17+
invm <- NULL
18+
set <- function(y) {
19+
x <<- y
20+
invm <<- NULL
21+
}
22+
get <- function() x
23+
setinverse <- function(m) invm <<- m
24+
getinverse <- function() invm
25+
list(set = set, get = get,
26+
setinverse = setinverse,
27+
getinverse = getinverse)
828
}
929

1030

11-
## Write a short comment describing this function
12-
31+
## Return a matrix that is the inverse of 'x' (created by makeCacheMatrix)
32+
## and caches it in the object x
1333
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
34+
invm <- x$getinverse()
35+
36+
if(!is.null(invm)) {
37+
message("getting cached data")
38+
return(invm)
39+
}
40+
41+
data <- x$get()
42+
invm <- solve(data, ...)
43+
x$setinverse(invm)
44+
invm
1545
}
46+

cachevector.R

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
cachemean <- function(x, ...) {
16+
m <- x$getmean()
17+
if(!is.null(m)) {
18+
message("getting cached data")
19+
return(m)
20+
}
21+
data <- x$get()
22+
m <- mean(data, ...)
23+
x$setmean(m)
24+
m
25+
}

0 commit comments

Comments
 (0)