[R] Kronecker matrix product
    Berwin A Turlach 
    berwin at maths.uwa.edu.au
       
    Wed Jul 13 11:37:37 CEST 2005
    
    
  
>>>>> "RH" == Robin Hankin <r.hankin at 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
========================== Full address ============================
Berwin A Turlach                      Tel.: +61 (8) 6488 3338 (secr)   
School of Mathematics and Statistics        +61 (8) 6488 3383 (self)      
The University of Western Australia   FAX : +61 (8) 6488 1028
35 Stirling Highway                   
Crawley WA 6009                e-mail: berwin at maths.uwa.edu.au
Australia                        http://www.maths.uwa.edu.au/~berwin
    
    
More information about the R-help
mailing list