Thanks Martin. That is exactly what I want.

Feng

On Sat, Jan 29, 2011 at 11:59 PM, Martin Maechler <
maechler@stat.math.ethz.ch> wrote:

> >>>>> "FL" == Feng Li <m@feng.li>
> >>>>>     on Sat, 29 Jan 2011 19:46:48 +0100 writes:
>
>    FL> I meant "sparse matrix", sorry for the typo.
> aha.. :-)
>
>
>    FL> On Sat, Jan 29, 2011 at 7:02 PM, Feng Li <m@feng.li> wrote:
>
>    >> Dear R,
>    >>
>    >> I have a simple question concerning with a special case of
>    >> sparse matrix multiplications. Say A is a 200-by-10000 dense
>    >> matrix. B is a 10000-by-10000 block- diagonal matrix, and each
>    >> diagonal block B_i is 100-by-100. The usual way I did A%*%B
>    >> will take about 30 seconds which is to time consuming because I
>    >> have to do this thousands of times. I also tried to partition A
>    >> into 100 small blocks and use mapply function to multiply by
>    >> each B_i, but that is even slower.
>    >>
>    >> I am wondering if there is an efficient way to perform this
>    >> type of multiplication with R?
>
> yes: e.g., via the (recommended, i.e. already installed) Matrix
> package's  bdiag() :
>
> require(Matrix)
> set.seed(1)
> A <- matrix(rnorm(2e6), 200, 10000)
> Blis <- lapply(1:100, function(i)matrix(rnorm(1e4), 100,100))
>
> system.time(B. <- .bdiag(Blis)) # 1.28 sec
> system.time(cc <- A %*% B.)     # 1.7  sec
> class(cc)# "dgeMatrix" .. i.e. dense
> ## and depending on the context you may revert to traditional unclassed
> matrices
> ## via
> c2 <- as(cc, "matrix")
>
>
>    >> Thanks in advance!
>
> you are welcome.
> Martin Maechler, ETH Zurich
>
>
>    >> Feng
>    >>
>    >> --
>    >> Feng Li Department of Statistics Stockholm University 106 91
>    >> Stockholm, Sweden http://feng.li/
>



-- 
Feng Li
Department of Statistics
Stockholm University
106 91 Stockholm, Sweden
http://feng.li/

	[[alternative HTML version deleted]]

