Skip to content

Commit 9196df3

Browse files
committed
First submission of the assignment
1 parent 7f657dd commit 9196df3

File tree

1 file changed

+37
-6
lines changed

1 file changed

+37
-6
lines changed

cachematrix.R

Lines changed: 37 additions & 6 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+
## A set of functions to create object to cache matrix inversion and to compute matrix inversion
2+
## makeCacheMatrix - creates special "matrix" pbject
3+
## cacheSolve - calculates inversion of th eobject created by makeCacheMatrix.
4+
## This function will also cache the result and returns cached calculations
5+
## if the inverse has already been calculated (and the matrix has not changed)
36

4-
## Write a short comment describing this function
7+
# makeCacheMatrix creates a special "vector", which is really a list containing a function to
8+
# set the value of the matric
9+
# get the value of the matrix
10+
# set the value of the inverted matrix
11+
# get the value of the inverted matrix
512

613
makeCacheMatrix <- function(x = matrix()) {
7-
14+
inverse <- NULL
15+
set <- function(y) {
16+
x <<- y
17+
inverse <<- NULL
18+
}
19+
20+
get <- function() x
21+
22+
setinverse <- function(i) inverse <<- i
23+
getinverse <- function() inverse
24+
25+
list(set = set, get = get, setinverse = setinverse, getinverse = getinverse)
826
}
927

1028

11-
## Write a short comment describing this function
29+
# cacheSolve receive a special "cacheMatrix" object, and then returns
30+
# cached inverted matrix if it was cached or comuputes new one, caches it and returns the result
1231

1332
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
33+
inverse <- x$getinverse()
34+
if(!is.null(inverse)) {
35+
message("getting cached data")
36+
return(inverse)
37+
}
38+
39+
matrix <- x$get()
40+
inverse <- solve(matrix)
41+
x$setinverse(inverse)
42+
inverse
1543
}
44+
45+
46+

0 commit comments

Comments
 (0)