[R] Fastest way to extract rows of smaller matrix many times by index to make larger matrix? and multiply columsn of matrix by vector

nevil amos nev||@@mo@ @end|ng |rom gm@||@com
Tue Sep 14 08:48:48 CEST 2021


Hi is there a faster way to "extract" rows of a matrix many times to for a
longer matrix based in a vector or for indices than M[ V, ]

I need to "expand" ( rather than subset)  a matrix M of 10-100,000 rows x
~50 columns to produce a matrix with a greater number (10^6-10^8) of rows
using a vector V containing the 10^6 -10^8 values that are the indices of
the rows of M. the output matrix M2 is then multiplied by another vector V2
With the same length as V.

Is there a faster way to achieve these calculations (which are by far the
slowest portion of a function looped 1000s of times? than the standard  M2
<- M[ V, ] and  M3<-M2*V2, the two calculations are taking a similar time,
Matrix M also changes for each loop.


M<-matrix(runif(50*10000,0,100),nrow=10000,ncol=50)
x = 10^7
V<-sample(1:10000,x,replace=T)
V2<-(sample(c(1,NA),x,replace=T))
print<-(microbenchmark(
M2<-M[V,],
M3<-M2*V2,
times=5,unit = "ms"))



thanks for any suggestions

Nevil Amos

	[[alternative HTML version deleted]]



More information about the R-help mailing list