Hi I want to write a little function that takes a matrix X of size m-by-n, and a list L of length "m", whose elements are matrices all of which have the same number of columns but possibly a different number of rows. I then want to get a sort of dumbed-down kronecker product in which X[i,j] is replaced by X[i,j]*L[[j]] where L[[j]] is the j-th of the "m" matrices. For example, if X = matrix(c(1,5,0,2),2,2) and L[[1]] = matrix(1:4,2,2) L[[2]] = matrix(c(1,1,1,1,1,10),ncol=2) I want [,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 > see how, for example, out[3:5,1:2] == 5*L[[2]], the "5" coming from X [2,1]. [ I can bind L together into a single matrix with do.call("rbind",L) and calculate the number of rows with sapply(L,nrow) but I don't see how this can help. ] -- Robin Hankin Uncertainty Analyst National Oceanography Centre, Southampton European Way, Southampton SO14 3ZH, UK tel 023-8059-7743
>>>>> "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
>>>>> "BT" == Berwin A Turlach <berwin at maths.uwa.edu.au> writes:>> tmp <- sapply(1:length(L), function(j, mat, list) kronecker(X[j,,drop=FALSE], L[[j]]), mat=X, list=L) Uups, should proof read more carefully before hitting the send button. This should be, of course: tmp <- sapply(1:length(L), function(j, mat, list) kronecker(mat[j,,drop=FALSE], list[[j]]), mat=X, list=L) 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