rdpeng / programmingassignment2 Goto Github PK
View Code? Open in Web Editor NEWRepository for Programming Assignment 2 for R Programming on Coursera
Repository for Programming Assignment 2 for R Programming on Coursera
makeCacheMatrix <- function(x = matrix()) {
inverted <- NULL
set <- function(y) {
x <<- y
inverted <<- NULL
}
get <- function() x
setInverted <- function(inverse) Inverted <<- inverse
getInverted <- function() Inverted
list(set = set, get = get,
setInverted = setInverted,
getInverted = getInverted)
}
cacheSolve <- function(x, ...) {
m <- x$getInverted()
if(!is.null(m)) {
message("getting cached data")
return(Inverted)
}
m <- x$get()
Inverted <- solve(m, ...)
x$setInverted(Inverted)
Inverted
}
It would be nice to have an valid usage and output example?
Maybe provided in form of a unit or test script.
Hi there,
I'm not sure how to figure out if my assignment has been submitted for review. I just finished Assignment 2 and submitted around 1:07. How do I verify?
Thank you...
makeCacheMatrix <- function(x = matrix()) {
inv <- NULL
set <- function(y) {
x <<- y
inv <<- NULL
}
get <- function() x
setInverse <- function(inverse) inv <<- inverse
getInverse <- function() inv
list(set = set,
get = get,
setInverse = setInverse,
getInverse = getInverse)
}
cacheSolve <- function(x, ...) {
## Return a matrix that is the inverse of 'x'
inv <- x$getInverse()
if (!is.null(inv)) {
message("getting cached data")
return(inv)
}
mat <- x$get()
inv <- solve(mat, ...)
x$setInverse(inv)
inv
}
makeCacheMatrix <- function(x = matrix()){
inver <- NULL
set <- function(y) {
x <<- y
inver <<- NULL
}
get <- function() x
setInver <- function(inver) inver <<- inver
getInver <- function()inver
list(set = set,
get = get,
setInver = setInver,
getInver = getInver)
}
cacheSolve <- function(x,...) {
inver <- x$getInver()
if(!is.null(inver)) {
message("getting cached data")
return(inver)
}
matri <- x$get()
inver <- solve(matri,...)
x$setInver(inver)
inver
}
makeVector <- function(x = matrix()) {
m <- NULL
set <- function(y) {
x <<- y
m <<- NULL
}
get <- function() x
setsolve <- function(solve) m <<- solve
getsolve <- function() m
list(set = set, get = get,
setsolve = setsolve,
getsolve = getsolve)
}
cachesolve <- function(x, ...) {
m <- x$getsolve()
if(!is.null(m)) {
message("getting cached data")
return(m)
}
data <- x$get()
m <- solve(data, ...)
x$setsolve(m)
m
}
The cacheSolve function is intended to return the inverse of a matrix. A clear, simple, objective. However, it's template has variable arguments (...) that are not further specified. That's confusing, because what is the intention? While it surely is possible to resolve this issue, IMHO it distracts from the core assignment - getting familiar with git(hub) and lexical scoping.
The standard approach (for newbies, at least) to deal with the arguments is to pass them to solve(). And cache the response as the inverse. While this is fine when no additional arguments are present on any of the invocations, imagine a vector argument b being there on the first invocation of cacheSolve. Not the inverse of the matrix m will be cached, but the solution x to m %*% x = b.
This might be related to the unfortunate name 'cacheSolve'. Such a name implies a more efficient implementation of solve when the same matrix is used repeatedly. But that's different from just getting the inverse. For the former definition, an additional argument b is to be expected - that's in the contract that solve() offers. The latter definition is a more specialized "solve" that only calculates the inverse, but doesn't offer the capability of solving a set of linear equations.
So IMO there are two ways to resolve the issue. One is to drop the ... argument and rename the function to cacheInverse (and include the hint to solve() in the comments). The other is to (only) support the b argument, with a default value that implies that the inverse should be returned.
makeCacheMatrix <- function(x = matrix()) {
## set the matrix
inv <-NULL
setMatrx<-function(mat){
m<<-mat
}
## get the matrix
getMatrix<-function() m
## set the inverse
cacheInverse<-function(temp){
inv<<-temp
}
## get the inverse
getInverse<-function(){
if (nrow(m) != ncol(m)) {print('matrix is not square')}
inv
}
list(setMatrix=setMatrix,getMatrix=getMatrix,cacheInverse=cacheInverse,getInverse=getInverse)
}
cacheSolve <- function(x) {
n<-s$getInverse()
if (!is.null(n)){
## get it from the cache and skips the computation.
message("getting cached data")
return(n)
}
## sets the value of the inverse in the cache via the setinv function.
n<-solve(s$getMatrix())
s$cacheInverse(n)
n
}
mywork.R.zip
makeCacheMatrix <- function(x = matrix())
{
inv=NULL
set=function(y)
{
x<<-y
inv<<-NULL
}
get=function(){return(x)}
setInverse=function(i){inv<<-i}
getInverse=function(){return(inv)}
output=list(set=set,get=get,setInverse=setInverse,getInverse=getInverse)
return(output)
}
cacheSolve <- function(x, ...)
{
inv=x$getInverse()
if(!is.null(inv))
{
message("getting cached data\n")
return(inv)
}
data=x$get()
inv=solve(data,...)
x$setInverse(inv)
return(inv)
}
hhu
makeCasheMatrix <- function(x=matrix()){
m <- matrix()
set <- function(y){
x <<- y
m <<- matrix()
}
get <- function() x
setinv<- function(inv) m <- inv
getinv <- function() m
list(set=set, get=get,
setinv=setinv,
getinv=getinv)
}
cacheSolve <- function(x,...){
m <- x$getinv()
if(!is.na(m)){
message("getting cache data")
return(m)
}
data <- x$get()
m <- solve(data)
x$setinv(m)
m
}
can't push my git branch to the GitHub repository my your account.
makeMatrix<-function(x=numeric()){
m<-NULL
set<-function(y){
x<<-y
m<<-NULL
}
get<-function() x
setinverse<-function(inverse) m<<-inverse
getinverse<-function() m
list(set=set,get=get,setinverse=setinverse,getinverse=getinverse)
}
cashemean<-function(x,...){
m<-x$getinverse()
if(!is.null(m)){
message("getting cashed data")
return(m)
}
data<-x$get()
m<-solve(data,...)
x$setinverse(m)
m
}
makeCacheMatrix <- function(x = matrix()){
inv <- NULL
set <- function(y){
x<<- y
inv<<- NULL
}
get <- function() x
setInv <- function(inverse) inv<<- inverse
getInv <- function() inv
list(set = set, get = get,
setInv = setInv,
getInv = getInv)
}
cacheSolve <- function(x, ...){
inv <- x$getInv()
if(!is.null(inv)){
message('getting cached data')
return(inv)
}
matrix <- x$get()
inv <- mean(matrix,...)
x$setInv(inv)
inv
}
The example implementation is (deeply?) flawed: cachemean
allows passing through additional parameters to mean
, but successive calls may have different parameters. Since the values are not checked for, the mean cached after the first call is returned even though it's (arguably) wrong:
> x <- makeVector(c(1:100,200))
> cachemean(x)
[1] 51.9802
> cachemean(x, trim=0.1)
getting cached data
[1] 51.9802
> mean(x$get(), trim=0.1)
[1] 51
Similar examples can be constructed with the na.rm
parameter of mean
.
Proposed solution: Drop the ...
parameter from cachedmean
.
(That is, if it is possible to check the parameters and/or cache one value per parameter combination, that may be a good solution as well. But 1) that's probably beyond the course at this point and 2) it may be a recipe for memory leaks.)
Make Cache Matrix:
makeCacheMatrix <- function(x = matrix()){
inv <- NULL
set <- function(y){
x<<- y
inv<<- NULL
}
get <- function() x
setInv <- function(solve) inv<<- solve
getInv <- function() inv
list(set = set, get = get,
setInv = setInv,
getInv = getInv)
Solve the matrix, and check the result (the product of the Source Matrix and its inverse must be 1).
cacheSolve <- function(x, ...){
inv <- x$getInv()
if(!is.null(inv)){
message('getting cached data')
return(inv)
}
sourcematrix <- x$get()
inv <- solve(sourcematrix,...)
x$setInv(inv)
inv
sourcematrix %*% inv
}
Sorry this is the wrong link!!!
The right link is https://github.com/bxjaj/Programming-Assignment-2-/tree/master
Please go the that link to check my assignment!
I can't change the link once I accidentally upload the wrong one, really sorry for that.
sapplymakeCacheMatrix <- function(x = matrix()) {
## set the matrix
## get the matrix
## set the inverse
## get the inverse
## this list is used as the input to cacheSolve()
inv_x <- NULL
set <- function(y) {
## use "<<-" to assign a value to an object in an environment
x <<- y
inv_x <<- NULL
}
get <- function() x
setinse<- function(inse) inv_x <<-inse
getinse <- function() inv_x
list(set = set, get = get,
setinse = setinse,
getinse = getinse)
}
cacheSolve <- function(x, ...) {
inv_x <- x$getinse()
if (!is.null(inv_x)) {
## get it from the cache and skips the computation.
message("getting cached inverse matrix")
return(inv_x)
} else {
inv_x <- solve(x$get())
## sets the value of the inverse in the cache via the setinv function.
x$setinse(inv_x)
return(inv_x)
}
}
makeCacheMatrix <- function(x = matrix()) {
inv <- NULL
set <- function(y) {
x <<- y
inv <<- NULL
}
get <- function() x
setInverse <- function(inverse) inv <<- inverse
getInverse <- function() inv
list(set = set,
get = get,
setInverse = setInverse,
getInverse = getInverse)
}
cacheSolve <- function(x, ...) {
inv <- x$getInverse()
if (!is.null(inv)) {
message("getting cached data")
return(inv)
}
mat <- x$get()
inv <- solve(mat, ...)
x$setInverse(inv)
inv
}
https://github.com/spguerrerot/ProgrammingAssignment2
makeCacheMatrix <- function(m = matrix()) {
inverse <- NULL
set <- function(y) {
m <<- y
inverse <<- NULL
}
get <- function() m
setInverse <- function(solve) inverse <<- solve
getInverse <- function() inverse
list(set = set, get = get,
setInverse = setInverse,
getInverse = getInverse)
}
cacheSolve <- function(m, ...) {
inverse <- m$getInverse()
if(!is.null(inverse)) {
message("getting cached data")
return(inverse)
}
data <- m$get()
inverse <- solve(data, ...)
m$setInverse(inverse)
inverse
}
cacheSolve <- function(x, ...) {
inv <- x$getinverse()
if(!is.null(inv)) {
message("getting cached data")
return(inv)
}
data <- x$get()
inv <- solve(data, ...)
x$setinverse(inv)
inv
}
makeCacheMatrix <- function(x = matrix()) {
int <- NULL
set <- function(a) {
dig <<- a
int <<- NULL
}
get <- function() dig
setinverse <- function(inverse) int <<- inverse
getinverse <- function() int
list(set = set, get = get,
setinverse = setinverse,
getinverse = getinverse)
}
cacheSolve <- function(dig, ...) {
int <- int$getinverse() ## Return a matrix that is the inverse of 'dig'
if(!is.null(int)) {
message("getting cached data")
return(int)
}
else{
data <- dig$get()
int <- mean(data, ...)
dig$setinverse(int)
int
}
}
cachematrix.txt
makeCacheMatrix<-function(x=matrix()){
m <- NULL
set <- function(y) {
x <<- y
m <<- NULL
}
get<-function()x
setsolve<-function(solve) m<<-solve
getsolve<-function() m
list(set=set,get=get,
setsolve=setsolve,
getsolve=getsolve)
}
cachesolve <- function(x) {
m <- x$getsolve()
if(!is.null(m)) {
message("getting cached data")
return(m)
}
data <- x$get()
m <- solve(data)
x$setsolve(m)
m
}
Hi,
I am new to Github, When I am trying to sync the committed changes I am getting an error stating "Sync Failed to push local changes - It seems you do not have permission to push your changes to this repository". Can anyone please help me with this, as today is the last day to complete and submit the assignment
Thanks in advance..
I realize that API styles are a matter of taste. FWIW, I think this is a nicer way of achieving caching functionality since it's harde to misuse:
makeVector <- function(x = numeric()) {
m <- NULL
set <- function(y) {
x <<- y
m <<- NULL
}
get <- function() x
mean <- function() {
if ( is.null(m) ) {
# Note that mean(x) would not work -- then the function would
# call itself!
m <<- base::mean(x)
}
m
}
list(set = set, get = get, mean = mean)
}
versioned here
makeCacheMatrix <- function(x = matrix()){
inv <- NULL
set <- function(y){
x<<- y
inv<<- NULL
}
get <- function() x
setInv <- function(inverse) inv<<- inverse
getInv <- function() inv
list(set = set, get = get,
setInv = setInv,
getInv = getInv)
}
cacheSolve <- function(x, ...){
inv <- x$getInv()
if(!is.null(inv)){
message('getting cached data')
return(inv)
}
matrix <- x$get()
inv <- mean(matrix,...)
x$setInv(inv)
inv
}
Solution to programming assignment 2
makeCacheMatrix <- function(x = matrix())
{
m <- matrix()
set <- function (y)
{
x <<- y
m <<- matrix()
}
get <- function() x
setinv <- function(inv) m <<- inverse
getinv <- function() m
list ( set = set, get = get, setinv = setinv, getinv = getinv)
}
cacheSolve <- function(x, ...)
{
m <- x$getinv()
if (!is.null(m))
{
message ("getting cached data")
return (m)
}
data <- x$get()
m <- solve(data)
x$setinv(m)
m
## Return a matrix that is the inverse of 'x'
}
makeVector<-function(x=numeric()){
cachemean<-function(x,...){
The assignment is worded as follows:
Write the following functions:
makeCacheMatrix
: This function creates a special "matrix" object that can cache its inverse.cacheSolve
: This function computes the inverse of the special "matrix" returned by
makeCacheMatrix above. If the inverse has already been calculated (and the matrix has not
changed), thencacheSolve
should retrieve the inverse from the cache.
Lest we assume that we should just copy-paste the example (which is, of course, essentially what this assignment is about, isn't it?) there are two things missing (comparing with the example):
set()
method as in the example?cacheSolve
change the given special matrix? In particular, should it store the value it computes? What should it do if the given object is not of the required form?makeCacheMatrix <- function(x = matrix())
{
m_inv <- NULL
set <- function(y)
{
x <<- y
m_inv <<- NULL
}
get <- function() x
setm_inv <- function(solve) m_inv <<- solve
getm_inv <- function() m_inv
list(set = set, get = get,
setm_inv = setm_inv,
getm_inv = getm_inv)
}
cacheSolve <- function(x, ...) {
m_inv <- x$getm_inv()
if(!is.null(m_inv)) {
message("getting cached data")
return(m_inv)
}
data <- x$get()
m_inv <- solve(data, ...)
x$setm_inv(m_inv)
m_inv
}
makeCacheMatrix <- function(x = matrix()) {
inv <- NULL
set <- function(y) {
x <<- y
inv <<- NULL
}
get <- function() x
setInverse <- function(inverse) inv <<- inverse
getInverse <- function() inv
list(set = set,
get = get,
setInverse = setInverse,
getInverse = getInverse)
}
cacheSolve <- function(x, ...) {
inv <- x$getInverse()
if (!is.null(inv)) {
message("getting cached data")
return(inv)
}
mat <- x$get()
inv <- solve(mat, ...)
x$setInverse(inv)
inv
}
I am unable to fork the programmingAssignement2 repository to my account. A popup window appears asking "where to fork this repsitory". I chose my account name, but nothing happens. Please help me in solving this issue..
I'm about to quit on this.
I've got the "cachematrix.R" code in the ProgrammingAssignment2 repository in the windows gitHub on my pc, and I think I've loaded it up on my online kd6888 gitHub account, but I have no idea what the last statement in the exercise means:
Submit to Coursera the URL to your GitHub repository that contains the completed R code for the assignment.
I can see in my online GitHub kd6888 repository that my hash code is 82d5b3c
but I've got no idea what to do after that to "submit" it to Coursera.
makeCacheMatrix <- function( x=matrix()){
matrix_inv <NULL
set <- function(y){
x <<- y
matrix_inv <<- NULL
}
get <- function() x
setinverse <- function( inverse) matrix_inv <<- inverse
getinverse <- function() matrix_inv
list(set = set, get = set,
setinverse = setinverse, getinverse = getinverse)
}
cacheSolve <- function(x, ...){
matrix_inv <- x$getinverse()
if(!is.null(matrix_inv)){
message("getting cached data")
return(matrix_inv)
}
my_matrix <- x$get()
matrix_inv <- solve(my_matrix, ...)
x$setinverse(matrix_inv)
inv
}
I am trying to upload my assignment 2 (cached matrix code) and I can not figure out what I am doing wrong. GitHub is not very user friendly!
makeCacheMatrix <- function(x = matrix()) {
m<-NULL
set<-function(y){
x<<-y
m<<-NULL
}
get<-function() x
setmatrix<-function(solve) m<<- solve
getmatrix<-function() m
list(set=set, get=get,
setmatrix=setmatrix,
getmatrix=getmatrix)
}
cacheSolve <- function(x, ...) {
## Return a matrix that is the inverse of 'x'
m<-x$getmatrix()
if(!is.null(m)){
message("getting cached data")
return(m)
}
matrix<-x$get
m<-solve(matrix, ...)
x$setmatrix(m)
m
}
I was able to fork/clone fine and have completed the assignment. How to I commit it and push it back here to review? This is very confusing/irritating. Thanks!
makeCacheMatrix <- function(x = matrix()) {
m<-NULL
set<-function(y){
x<<-y
m<<-NULL
}
get<-function() x
setmatrix<-function(solve) m<<- solve
getmatrix<-function() m
list(set=set, get=get,
setmatrix=setmatrix,
getmatrix=getmatrix)
}
cacheSolve <- function(x=matrix(), ...) {
m<-x$getmatrix()
if(!is.null(m)){
message("getting cached data")
return(m)
}
matrix<-x$get
m<-solve(matrix, ...)
x$setmatrix(m)
m
}
makeCacheMatrix <- function(x = numeric()) {
matinv <- NULL
set <- function(y) {
x <<- y
matinv <<- NULL
}
get <- function() x
setinv <- function(inverse) matinv <<- inverse
getinv <- function() matinv
list(set = set,
get = get,
setinv = setinv,
getinv = getinv)
}
cacheSolve <- function(x, ...) {
matinv <- x$getinv()
if(!is.null(matinv)) {
message("getting cached data")
return(inv)
}
data <- x$get()
inv <- solve(data, ...)
x$setinv(inv)
inv
}
Test
makeCacheMatrix <- function(x = mat()) {
a<-NULL
set<-function(y){
x<<-y
a<<-NULL
}
get<-function() x
setmat<-function(solve) a<<- solve
getmat<-function() a
list(set=set, get=get,
setmat=setmat,
getmat=getmat)
}
cacheSolve <- function(x=mat(), ...) {
a<-x$getmat()
if(!is.null(a)){
message("getting cached data")
return(a)
}
mat<-x$get
a<-solve(mat, ...)
x$setmat(a)
a
}
Please if some one can walk me through the process of submitting this assignment.
If someone can write me step by step from the start would be appreciated.
Thanks.
Please help. I made two commits from my local repository to the master. ( commit, because, push is not working ). I have 2 SHA1 keys. Where can I see these and how can this be considered as submitted ?????
I am getting an error saying "SSL certificate problem: unable to get local issuer certificate"
makeCacheMatrix <- function(x = matrix()) {
inv <- NULL
set <- function(y) {
x <<- y
inv <<- NULL
}
get <- function() x
setinverse <- function(solve) inv <<- solve
getinverse <- function() inv
list(set = set, get = get,
setinverse = setinverse,
getinverse = getinverse)
}
makeCacheMatrix <- function(x = matrix()) {
i <- NULL
set <- function(y){
x <<- y
i <<- NULL
}
get <- function() x
setInv <- function(inv){
i <<- inv
}
getInv <- function() i
list(set = set, get = get,
setInv = setInv,
getInv = getInv)
}
cacheSolve <- function(x, ...) {
## Return a matrix that is the inverse of 'x'
i <- x$getInv()
if(!is.null(i)){
message("getting cached data")
return(i)
}
data <- x$get()
i <- solve(data, ...)
x$setInv(i)
i
}
makeCacheMatrix <- function(x = matrix()) {
inv <- NULL
set <- function(y) {
x <<- y
inv <<- NULL
}
get <- function() x
setinverse <- function(solve) inv <<- solve
getinverse <- function() inv
list(set = set, get = get,
setinverse = setinverse,
getinverse = getinverse)
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.