[R] fast matrix update

Duncan Murdoch murdoch at stats.uwo.ca
Wed May 18 12:07:12 CEST 2005

vincent wrote:
> Hello,
> I use extensively m[i0:i1,j0:j1] = ... to update matrices parts.
> This writing is very convenient, but, since I'm doing this
> calculus many many times, I would like to know if there is a way
> (a hope ?) to do the same operation faster ?

It tends to be faster to use vector indexing rather than matrix 
indexing, so if you can calculate which entries correspond to the block 
you want to replace you might find this is faster:

block <- [ some calculation here, e.g. i0:i1 if j0 = j1 = 1 ]
m[block] <- ...

Of course, the calculation involved in the general case is ugly and 
could be slow.  If this is really a crucial bottleneck to your 
calculation, I'd suggest skipping over that optimization and redoing the 
function in C or Fortran.

Duncan Murdoch

More information about the R-help mailing list