[R] Binary Matrices

Barry Rowlingson B.Rowlingson at lancaster.ac.uk
Wed Apr 13 19:23:38 CEST 2005


Martin Maechler wrote:

> As others have said, you can use either 'SparseM' or 'Matrix'.
> The latter has also good code for sparse matrices, and actually
> I know that Doug Bates was going to implement sparse logical
> matrices in the next few days.

  The old 8-bit assembly language programmer in me baulks at this 
flippant waste of memory. Storing a *single bit* in 4 bytes? That would 
reduce the maximum number of bits you could store on a Z80-based machine 
with a fully maxed-out 64k of RAM to 16384, minus the 8k or whatever 
your system ROM was.

  No, the real solution to this problem lies in the 'rawToChar' 
functions, and its friends. You could pack your entire binary matrix 
into a character string, and store 8 bits per byte. A factor of 32 
improvement in storage (plus a little overhead).

  You could go the whole hog and write a new binary matrix class, but 
that would probably be silly, much easier to just write two functions 
that packed a binary matrix into this form, and unpacked it back into 
full numeric matrix form.

  And of course this method doesn't rely on the matrix being sparse, or 
use run-length encoding or other compression scheme. Its a flat factor 
of 32 compression.

  Okay, I'm being slightly sarcastic here, and if it wasn't time to go 
home now I'd have a quick bash at this, just to see what you can do with 
R's rawToChar function and friends. Never knew they existed until now.

Baz




More information about the R-help mailing list