8000 Add files via upload · fortmanj/ProgrammingAssignment2@c3f314c · GitHub 10000
[go: up one dir, main page]

Skip to content

Commit c3f314c

Browse files
authored
Add files via upload
Final upload of Assignment rdpeng#2 for R programming Coursera Course
1 parent f32bd92 commit c3f314c

File tree

1 file changed

+35
-11
lines changed

1 file changed

+35
-11
lines changed

ProgrammingAssignment2.R

Lines changed: 35 additions & 11 deletions
< 8EF6 td data-grid-cell-id="diff-17f1cd87465b0b799fd74a20eed65a58a1c7f356d21dd1f500aa88eb4e6330ec-42-52-1" data-selected="false" role="gridcell" style="background-color:var(--diffBlob-additionNum-bgColor, var(--diffBlob-addition-bgColor-num));text-align:center" tabindex="-1" valign="top" class="focusable-grid-cell diff-line-number position-relative left-side">52
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
## Put comments here that give an overall description of what your
22
## functions do
33

4-
## Write a short comment describing this function
4+
## Write a short comment describing this function:
55
##There are two functions makeCacheMatrix,makeCacheMatrix
66
##makeCacheMatrix consists of set,get,setinv, getinv
77
##library(Mass) is used to calculate inverse for non squared as well as squared matricies
88

99
library(MASS)
1010
makeCacheMatrix <- function(x = matrix()) {
1111
inv<-NULL #initializing inverve as NULL
12-
set <- function(y) {
12+
set <- function(y){
1313
x <<- y
14-
inv <<- NULL
15-
}
16-
get <- function() x #function to get matrix x
17-
setinv <- function(mean) inv <<- inverse
18-
getinv <- function() {
14+
inv <<-NULL
15+
}
16+
get <- function()x #function to get matrix x
17+
setinv <- function(inverse) inv <<- inverse
18+
getinv <- function(){
1919
inver<-ginv(x)
20-
inver%%x #function to obtain inverse of the matrix
20+
inver%*%(x) #function to obtain inverse of the matrix
2121
}
2222
list(set = set, get = get,
2323
setinv = setinv,
@@ -28,16 +28,40 @@ makeCacheMatrix <- function(x = matrix()) {
2828
## Write a short comment describing this function
2929
##This is used to get the cache data
3030

31-
cacheSolve <- function(x, ...) ##gets cache data
31+
cacheSolve <- function(x,...) ##gets cache data
3232
{
3333
inv<-x$getinv()
3434
if(!is.null(inv)){ #checking whether inverse if Null
35-
message("getting chached data!")
36-
return(inv) #returns inverse value
35+
message("getting chached data!")
36+
return(inv) #returns inverse value
3737
}
3838
data<-x$get()
3939
inv<-solve(data,...) #calculates inverse value
4040
x$setinv(inv)
4141
inv ## Return a matrix that is the inverse of 'x'
4242
}
43+
44+
##testing code:
45+
46+
> f<-makeCacheMatrix(matrix(1:8,2,4))
47+
> f$get()
48+
[,1] [,2] [,3] [,4]
49+
[1,] 1 3 5 7
50+
[2,] 2 4 6 8
51+
+
> f$getinv()
53+
[,1] [,2] [,3] [,4]
54+
[1,] 0.7 0.4 0.1 -0.2
55+
[2,] 0.4 0.3 0.2 0.1
56+
[3,] 0.1 0.2 0.3 0.4
57+
[4,] -0.2 0.1 0.4 0.7
58+
59+
> cacheSolve(f)
60+
getting chached data!
61+
[,1] [,2] [,3] [,4]
62+
[1,] 0.7 0.4 0.1 -0.2
63+
[2,] 0.4 0.3 0.2 0.1
64+
[3,] 0.1 0.2 0.3 0.4
65+
[4,] -0.2 0.1 0.4 0.7
66+
4367

0 commit comments

Comments
 (0)
0