Dear David, and R-users, thanks for the response. I'll do my best to describe the context.
My data consists in two matrices, D and T. The former provides simple values, the latter is a Boolean matrix. The number of rows in D is equal to the number of columns in T. My aim is to construct a larger matrix which is made up of sum(T) diagonal matrices constructed from the rows of D, and placed according to a structure given by a transformation of T. Whereas the positions of the columns in which T is equal to 1 informs about the placement of the diagonal matrices, the positions of the rows in which T is equal to 1 informs about which row to take from D in order to create these diagonal matrices. In the code below (corrected in the last line with respect to the previous version, sorry), I first created the matrices D and T. Then I built a 0/1 matrix (M0) that presents the structure of the final block-matrix made up of diagonal matrices, and, by Kronecker product, a matrix with the final 0/1 structure is constructed, M. Using the rows in which T is equal to 1, I select the values from D and place in the matrix M, where such matrix is equal to 1. It's a long explanation and I could not find a better way to program (and explain) it. Thanks for you help, Giancarlo ## data in matrices D <- matrix(1:15, 3, 5) T <- matrix(0, 3, 3) T[c(2,4,6,8)] <- 1 ## the col of T equal to 1 gives the position wr <- which(T==1, arr.ind=TRUE)[,2] ## we aim to sum(T) diagonal matrices wc <- 1:sum(T) ## structure: how to place the diagonal matrices M0 <- matrix(0, nrow(T), sum(T)) M0[cbind(wr,wc)] <- 1 ## number of columns m <- ncol(D) ## final 0/1 matrix M <- kronecker(M0, diag(m)) ## the row of T equal to 1 gives which rows to take from D pos <- which(T==1, arr.ind=TRUE)[,1] ## filling up with data M[M!=0] <- t(D[pos,]) On 29/01/2014 01:49, David Winsemius wrote: > On Jan 27, 2014, at 8:04 AM, Carlo Giovanni Camarda wrote: > >> Dear R-users, >> >> I would like to know whether you know some trick for skipping some of >> the steps in the example below (especially the last step in a way that >> would make easier to be written succinctly in a text). >> >> I could try to explain in words the whole process, but I'm sure the code >> below would be clearer. > After looking at the code and output, I must disagree. The lack of any > response from the rest of the readership suggests to me that I am not the > only one who thinks a natural language description of the context and goals > for this effort would help. > >> Thanks in advance for your help, >> Giancarlo >> >> >> ## data in matrices >> D <- matrix(1:15, 3, 5) >> T <- matrix(0, 3, 3) >> T[c(2,4,6,8)] <- 1 >> >> ## how to place the diag matrices of each row >> M0 <- matrix(0, nrow(T), sum(T)) >> wr <- which(T==1, arr.ind=TRUE)[,2] >> wc <- 1:ncol(M0) >> M0[cbind(wr,wc)] <- 1 >> >> ## number of columns >> m <- ncol(D) >> ## non-zero positions >> M <- kronecker(M0, diag(m)) >> ## which rows to take >> pos <- which(T==1, arr.ind=TRUE)[,1] >> ## filling up with data >> M[M!=0] <- t(D[wr,]) >> >> [[alternative HTML version deleted]] >> >> ______________________________________________ >> R-help@r-project.org mailing list >> https://stat.ethz.ch/mailman/listinfo/r-help >> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html >> and provide commented, minimal, self-contained, reproducible code. > David Winsemius > Alameda, CA, USA > [[alternative HTML version deleted]] ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.