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

Skip to content

Commit 022328a

Browse files
committed
Implemented cachematrix.R
1 parent 7f657dd commit 022328a

File tree

2 files changed

+132
-9
lines changed

2 files changed

+132
-9
lines changed

.Rapp.history

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
my_score <- c(16,20,20,36)
2+
max_score <- c(20,20,20,41)
3+
weight <- c(20,20,20,40)
4+
grade_achieved <- 100 * weighted.mean(my_score / max_score, weight)
5+
grade_achieved
6+
getwd()
7+
dir()
8+
x <- 2
9+
attributes(x)
10+
x
11+
attributes()
12+
attributes(x)
13+
q()
14+
x <- 4
15+
class(x)
16+
x <- c(4,"a",TRUE)
17+
class(x)
18+
x <- c("a","b","c")
19+
seq_along(x)
20+
rbinom(1, 1, 0.5)
21+
cube <- function(x, n) {}
22+
cube <- function(x, n) {
23+
x^3
24+
}
25+
cube(3)
26+
x <- 1:10#
27+
if(x > 5) {#
28+
x <- 0#
29+
}
30+
f <- function(x) {#
31+
g <- function(y) {#
32+
y + z#
33+
}#
34+
z <- 4#
35+
x + g(x)#
36+
}
37+
z <- 10
38+
f(3)
39+
x <- 5#
40+
y <- if(x < 3) {#
41+
NA#
42+
} else {#
43+
10#
44+
}
45+
y
46+
h <- function(x, y = NULL, d = 3L) {#
47+
z <- cbind(x, d)#
48+
if(!is.null(y))#
49+
z <- z + y#
50+
else#
51+
z <- z + f#
52+
g <- x + y / z#
53+
if(d == 3L)#
54+
return(g)#
55+
g <- g + 10#
56+
g#
57+
}
58+
getwd()
59+
setwd("/Users/ramaro/GIT/ProgrammingAssignment2/")
60+
getwd()
61+
c <- rbind(c(1, -1/4), c(-1/4, 1))
62+
c
63+
ic <- solve(c)
64+
ic
65+
ic <- solve(c) * c
66+
ic
67+
ic <- solve(c) %*% c
68+
ic
69+
library("MASS")
70+
ic <- ginv(c)
71+
ic
72+
c <- rbind(c(1, -1), c(-1, 1))
73+
c
74+
ic <- solve(c)
75+
ic <- ginv(c)
76+
ic
77+
ic <- solve(c)
78+
det(c)
79+
b <- rbind(c(3,6,3),c(5,2,1),c(1,2,1))
80+
b
81+
det(b)
82+
solve(b)
83+
ginv(b)
84+
source(cachematrix.R)
85+
source("cachematrix.R")
86+
ma <- makeCacheMatrix(rbind(c(1, -1/4), c(-1/4, 1)))
87+
ma
88+
cacheSolve(ma)
89+
cacheSolve(ma)
90+
ma$get()
91+
ma <- makeCacheMatrix(c)
92+
cacheSolve(ma)
93+
ma <- makeCacheMatrix(b)
94+
cacheSolve(ma)
95+
source(cachematrix.R)
96+
source("cachematrix.R")
97+
cacheSolve(ma)
98+
ma <- makeCacheMatrix(rbind(c(1, -1/4), c(-1/4, 1)))
99+
cacheSolve(ma)
100+
ma <- makeCacheMatrix(c)
101+
cacheSolve(ma)

cachematrix.R

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,37 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
3-
4-
## Write a short comment describing this function
1+
## Computes the inverse of the "matrix"
2+
## if the inverse has already been calculated (and the matrix has not changed),
3+
## then the cachesolve should retrieve the inverse from the cache
54

5+
## This function creates a speacial "matrix" object that can cache its inverse
66
makeCacheMatrix <- function(x = matrix()) {
7-
7+
mi <- NULL
8+
set <- function(y){
9+
x <<- y
10+
mi <<- NULL
11+
}
12+
get <- function() x
13+
setinverse <- function(minverse) mi < minverse
14+
getinverse <- function() mi
15+
list(set = set, get = get,
16+
setinverse = setinverse,
17+
getinverse = getinverse)
818
}
919

10-
11-
## Write a short comment describing this function
12-
20+
## Computing the inverse of a square matrix
1321
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
22+
## Return a matrix that is the inverse of 'x'
23+
mi <- x$getinverse()
24+
if (!is.null(mi)) {
25+
return(mi)
26+
}
27+
m <- x$get()
28+
29+
d <- det(m)
30+
31+
if(d > 0) {
32+
mi <- solve(m)
33+
x$setinverse(mi)
34+
}
35+
36+
mi
1537
}

0 commit comments

Comments
 (0)