Coder Social home page Coder Social logo

r-programming's Introduction

The following code demonstrates how to use the makeCacheMatrixMean.R script.

Lines starting with # are simple comments, lines starting with #> are things printed in the output.

read the R script

change the directory to the directory of the file you saved

source("makeCacheMatrixMean.R")

create a square matrix (because solve only handles square matrices)

create the matrix during the call of makeCacheMatrix()

a <- makeCacheMatrix( matrix(c(1,2,12,13), nrow = 2, ncol = 2) );

summary(a); #> Length Class Mode
#> setMatrix 1 -none- function #> getMatrix 1 -none- function #> cacheInverse 1 -none- function #> getInverse 1 -none- function

a$getMatrix(); #> [,1] [,2] #> [1,] 1 12 #> [2,] 2 13

cacheSolve(a) #> [,1] [,2] #> [1,] -1.1818182 1.09090909 #> [2,] 0.1818182 -0.09090909

the 2nd time we run the function,we get the cached value

cacheSolve(a) #> getting cached data #> [,1] [,2] #> [1,] -1.1818182 1.09090909 #> [2,] 0.1818182 -0.09090909 Alternatively, the matrix can be created after calling a makeCacheMatrix without arguments.

read the R script

replace the "path/to/file" with the directory you save the file into

or you can read the file directly from the web

source("path/to/file/assessment3.R")

call makeCacheMatrix without arguments

a <- makeCacheMatrix(); summary(a); #> Length Class Mode
#> setMatrix 1 -none- function #> getMatrix 1 -none- function #> cacheInverse 1 -none- function #> getInverse 1 -none- function

create a square matrix (reason solve only handles square matrices )

#if the matrix is not square,it will prompt like this :

a<-makeCacheMatrix() summary(a) Length Class Mode
setMatrix 1 -none- function getMatrix 1 -none- function cacheInverse 1 -none- function getInverse 1 -none- function a$setMatrix(matrix(c(1,2,3,4,5,6),nrow=3,ncol=2)) a$getMatrix() [,1] [,2] [1,] 1 4 [2,] 2 5 [3,] 3 6 cacheSolve(a) #"Error in solve.default(data) : 'a' (3 x 2) must be square"

a$setMatrix( matrix(c(1,2,12,13), nrow = 2, ncol = 2) ); a$getMatrix(); #> [,1] [,2] #> [1,] 1 12 #> [2,] 2 13

cacheSolve(a) #> [,1] [,2] #> [1,] -1.1818182 1.09090909 #> [2,] 0.1818182 -0.09090909

the 2nd time we run the function, we get the cached value

cacheSolve(a) #> getting cached data #> [,1] [,2] #> [1,] -1.1818182 1.09090909 #> [2,] 0.1818182 -0.09090909

r-programming's People

Contributors

wyp22215 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.