-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcachematrix.R
More file actions
56 lines (50 loc) · 1.52 KB
/
cachematrix.R
File metadata and controls
56 lines (50 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
## Programming assignment 2 for the coursera cource on R-programming
## This function will cache the inverse of the entered matrix.
##When the number of columns and rows in the matrix is not equal,
##it will return an error saying the matrix is not a square
##(which is a prerequisite for the solve() function).
makeCacheMatrix <- function(x) {
CacheMatrix<-NULL
if(nrow(x)==ncol(x)){
CacheMatrix<<-solve(x)
}
if(nrow(x)!=ncol(x)){
print("Matrix is not a square")
}
}
## The cacheSolve generates the inverse of the entered matrix.
## If this has already been performed by the makeCacheMatrix function,
## the previously calculated inverse matrix is returned.
cacheSolve <- function(x) {
if (exists("CacheMatrix")){
if(solve(x) == CacheMatrix){
print("returning cached matrix")
print(CacheMatrix)
}
}
if (exists("CacheMatrix")){
if(solve(x) != CacheMatrix){
print("Reverse new input matrix not the same as cached inverse matrix")
print("Calculating new inverse matrix")
NewMatrix<-NULL
if(nrow(x)==ncol(x)){
NewMatrix<<-solve(x)
print(head(NewMatrix))
}
if(nrow(x)!=ncol(x)){
print("New input matrix is not a square")
}
}
}
if(!exists("CacheMatrix")){
print("Calculating new inverse matrix")
NewMatrix<-NULL
if(nrow(x)==ncol(x)){
NewMatrix<<-solve(x)
print(head(NewMatrix))
}
if(nrow(x)!=ncol(x)){
print("New input matrix is not a square")
}
}
}