From 48825a3cc92acaf825e09bd4175c9940afc3dabc Mon Sep 17 00:00:00 2001 From: dalawad Date: Tue, 8 Apr 2025 21:26:08 -0700 Subject: [PATCH 1/2] completed function --- cachematrix.R | 39 +++++++++++++++++++++++++++++---------- 1 file changed, 29 insertions(+), 10 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..02191d815db 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,34 @@ -## Put comments here that give an overall description of what your -## functions do +# This script does 2 things: +# 1. it stores data into an object and initializes a "storage space" for a computation performed on said data +# 2. it performs computation on the vector, returns an answer, and stores the answer in the storage space -## Write a short comment describing this function - -makeCacheMatrix <- function(x = matrix()) { +################################################################################ +# Assignment 2 +################################################################################ +makeCacheMatrix <- function(x = matrix()) { # The function where data is stored and "stotage" or cache is initialized + inv <- NULL + set <- function(y) { + x <<- y + m <<- NULL + } + get <- function() x + setinv <- function(inverse) inv <<- inverse + getinv <- function() inv + list(set = set, get = get, + setinv = setinv, + getinv = getinv) } - -## Write a short comment describing this function - -cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' +cacheSolve <- function(x, ...) { # A function that solves the inverse of a matrix 'x', the inverse is stored in makeCasheMatrix for fast retrival + inv <- x$getinv() + if(!is.null(inv)) { + message("getting cached data") + return(inv) + } + data <- x$get() + inv <- solve(data, ...) + x$setinv(inv) + inv } + From d90d8c79439cf7f4cd065aa725a3cce22c7cd566 Mon Sep 17 00:00:00 2001 From: dalawad Date: Wed, 9 Apr 2025 11:52:42 -0700 Subject: [PATCH 2/2] Update cachematrix.R updated script. Naming correction (m to inv) --- cachematrix.R | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index 02191d815db..9c0b3608767 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -10,7 +10,7 @@ makeCacheMatrix <- function(x = matrix()) { # The function where data is stored inv <- NULL set <- function(y) { x <<- y - m <<- NULL + inv <<- NULL } get <- function() x setinv <- function(inverse) inv <<- inverse @@ -23,7 +23,7 @@ makeCacheMatrix <- function(x = matrix()) { # The function where data is stored cacheSolve <- function(x, ...) { # A function that solves the inverse of a matrix 'x', the inverse is stored in makeCasheMatrix for fast retrival inv <- x$getinv() if(!is.null(inv)) { - message("getting cached data") + message("getting cached inverse") return(inv) } data <- x$get()