[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 11:10:46 CEST 2021
OK thanks, I thought it probably was, but always worth asking. the
multiplication of the columns of M2 by V2 is as intended - not matrix
multiplication.
On Tue, 14 Sept 2021 at 17:49, Jeff Newmiller <jdnewmil using dcn.davis.ca.us>
wrote:
> That is about as fast as it can be done. However you may be able to avoid
> doing it at all if you fold V2 into a matrix instead. Did you mean to use
> matrix multiplication in your calculation of M3?
>
> On September 13, 2021 11:48:48 PM PDT, nevil amos <nevil.amos using gmail.com>
> wrote:
> >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]]
> >
> >______________________________________________
> >R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> >https://stat.ethz.ch/mailman/listinfo/r-help
> >PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> >and provide commented, minimal, self-contained, reproducible code.
>
> --
> Sent from my phone. Please excuse my brevity.
>
[[alternative HTML version deleted]]
More information about the R-help
mailing list