[R] Kronecker matrix product
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,]1300 [2,]2400 [3,]5522 [4,]5522 [5,]5 502 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 __ 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
Re: [R] Kronecker matrix product
RH == Robin Hankin [EMAIL PROTECTED] 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,]1300 RH [2,]2400 RH [3,]5522 RH [4,]5522 RH [5,]5 502 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,]1300 [2,]2400 [3,]5522 [4,]5522 [5,]5 502 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 6009e-mail: [EMAIL PROTECTED] Australiahttp://www.maths.uwa.edu.au/~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
Re: [R] Kronecker matrix product
BT == Berwin A Turlach [EMAIL PROTECTED] 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 6009e-mail: [EMAIL PROTECTED] Australiahttp://www.maths.uwa.edu.au/~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