Vectors-and-loops-in-R-continued

This is a follow-up to an earlier post Vectorized functions vs loops in R.

The real-world math problem of my friend can be explained like this. In a large matrix filled with zeroes, start from 0,0 and find the nearest zero and set it to one. From this cell, set 2 in cells in rows and columns according to a predefined pattern. And do not bother about cells that fall outside of these two lines:

To give you an idea of how it looks, see here for the result for numbers up to 13: (unfinished)

The interesting cells are the ones set to 1, and here is a plot for the first 100. (unfinished)

The first solution consisted of a three-level nested loop, but as my friend wanted to know what this plot looked like for numbers up to 30.000 we searched for ways of optimising the calculations. But by substituting the innermost loop to a vectorised calculation we saved a lot of computational time. [Show figures, and code]

Still, a 30.000 by 30.000 matrix was more than R managed on our hardware (which is not limited to my 1997 laptop). But since only a small part of the matrix is actually ever filled with something, we could do with a sparse matrix instead. A sparse matrix (function) saves memory by keeping record of all defined cells, and not allocating memory for undefined cells. As long as the proportion of undefined (zeroed) cells is large, this method can save a lot of memory.

comments powered by Disqus


Back to the index

Blog roll

R-bloggers, Debian Weekly
Valid XHTML 1.0 Strict [Valid RSS] Valid CSS! Emacs Muse Last modified: oktober 17, 2019