Hi all, I am looking for a function like cumprod() that works for matrix multiplication. In other words, I have matrices [M1, M2, ..., Mn], and I want to calculate [M1, M1%*%M2, M1%*%M2%*%M3, ..., M1%*%...%*%Mn] as quickly as possible. Right now I'm using a for() loop but it seems like there should be a faster way. Any help is appreciated! Thanks, Todd Schneider todd.w.schneider@gmail.com [[alternative HTML version deleted]]
Don't know if its any faster but try this:
Reduce("%*%", list(M1, M2, M3), accumulate = TRUE)
On Thu, Oct 29, 2009 at 9:56 AM, Todd Schneider
<todd.w.schneider at gmail.com> wrote:> Hi all,
>
> I am looking for a function like cumprod() that works for matrix
> multiplication.
>
> In other words, I have matrices [M1, M2, ..., Mn], and I want to calculate
> [M1, M1%*%M2, M1%*%M2%*%M3, ..., M1%*%...%*%Mn] as quickly as possible.
> Right now I'm using a for() loop but it seems like there should be a
faster
> way.
>
> Any help is appreciated!
>
> Thanks,
>
> Todd Schneider
> todd.w.schneider at gmail.com
>
> ? ? ? ?[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> 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.
>
If the matrices are not all the same size, then the order of computation will make a difference. simple example: A is 1xn, B is nx1, C is 1xn. A(BC) takes n^3 multiplies, while (AB)C requires 2n. albyn Quoting Todd Schneider <todd.w.schneider at gmail.com>:> Hi all, > > I am looking for a function like cumprod() that works for matrix > multiplication. > > In other words, I have matrices [M1, M2, ..., Mn], and I want to calculate > [M1, M1%*%M2, M1%*%M2%*%M3, ..., M1%*%...%*%Mn] as quickly as possible. > Right now I'm using a for() loop but it seems like there should be a faster > way. > > Any help is appreciated! > > Thanks, > > Todd Schneider > todd.w.schneider at gmail.com > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > 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. > >