Coursera: R Programming Week 3 Tips

The week 3’s assignment is the easiest one. If you can figure out the sample program provided, the solution is very similar to the sample code. You only need to replace a few strings to get your assignment done.

Someone in the class forum wrote a small program to verify the assignment. I have modified it and here is the listing. BTW, I do not have access to the forum any more and I do not know who is the original author. If you are the original author, please let me know and I’ll mention your name in this post. Once you are done coding, put this code in the same directory and source it.

# Test your code
source("cachematrix.R")
#
# generate matrix, and the inverse of the matrix.
size mymatrix mymatrix.inverse #
# now solve the matrix via the cache-method
#
special.matrix #
# this should take long, since it's the first go
print("Solving the matrix for the first time.")
time1 special.solved.1 time2 print(time2 - time1)
#
# this should be lightning fast
print("Solving the matrix for the second time.")
time1 special.solved.2 time2 print(time2 - time1)
#
# check if all solved matrices are identical
print(identical(mymatrix.inverse, special.solved.1))
print(identical(mymatrix.inverse, special.solved.2))
#
# should return TRUE