Re: [R] Kronecker matrix product

From: Berwin A Turlach <berwin_at_maths.uwa.edu.au>
Date: Wed 13 Jul 2005 - 19:37:37 EST

>>>>> "RH" == Robin Hankin <r.hankin@noc.soton.ac.uk> writes:

    RH> I want to write a little function that takes a matrix X of
    RH> size m-by-n, and a list L of length "m", whose elements are
    RH> matrices all of which have the same number of columns but
    RH> possibly a different number of rows.

    RH> I then want to get a sort of dumbed-down kronecker product in which     RH> X[i,j] is replaced by X[i,j]*L[[j]]

    RH> where L[[j]] is the j-th of the "m" matrices. For example, if

    RH> X = matrix(c(1,5,0,2),2,2)

    RH> and

    RH> L[[1]] = matrix(1:4,2,2)
    RH> L[[2]] = matrix(c(1,1,1,1,1,10),ncol=2)

    RH> I want

    RH> [,1] [,2] [,3] [,4]
    RH> [1,]    1    3    0    0
    RH> [2,]    2    4    0    0
    RH> [3,]    5    5    2    2
    RH> [4,]    5    5    2    2
    RH> [5,]    5   50    2   20

> tmp <- sapply(1:length(L), function(j, mat, list) kronecker(X[j,,drop=FALSE], L[[j]]), mat=X, list=L)
> do.call("rbind", tmp)

     [,1] [,2] [,3] [,4]

[1,]    1    3    0    0
[2,]    2    4    0    0
[3,]    5    5    2    2
[4,]    5    5    2    2
[5,]    5   50    2   20

>

HTH. Cheers,

        Berwin


R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Wed Jul 13 19:41:30 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:33:37 EST