I have 2 matrices, A (1000x21) and B (1000x7). Matrix A has individuals(=1000) in the rows and their consumption in 21 days at the columns. Matrix B has the SAME individuals(=1000) in the rows and some weights for each day of the week(=7) in the columns.

What I would like to have in the end is 1000 (with the dimension of 2x21) matrices (one for each individual), lets call them $X_{i}$. In the first row of each $X_{i}$ I would like to have the consumption of the individual $i$ each of the 21 days (this will come from matrix A), and at the second row of the $X_{i}$ I would like to have the respective weight of that day (this will come from matrix B).

So matrix A looks like $[cons_{1,1} \ cons_{1,2} \ ... \ cons_{1,21} \\ \ cons_{2,1} \ cons_{2,2} \ ... \ cons_{2,21} \\ . \\ . \\ . \\ \ cons_{1000,1} \ cons_{1000,2} \ ... \ cons_{1000,21}] $

Matrix B looks like $[weight_{1,1} \ weight_{1,2} \ ... \ weight_{1,7} \\ \ weight_{2,1} \ weight_{2,2} \ ... \ weight_{2,7} \\ . \\ . \\ . \\ \ weight_{1000,1} \ weight_{1000,2} \ ... \ weight_{1000,7}]$

And I would like the matrix $X_{i}$ to be like $[cons_{i,1} \ cons_{i,2} \ ... \ cons_{i,21} \\ \ weight_{i,1} \ weight_{i,2} \ ... weight_{i,k}]$

Any ideas how to do this in R in a loop ?

  • $\begingroup$ Do you want to have 1000 separate matrix objects or have them in a list? $\endgroup$
    – Jan Sila
    Aug 18 '16 at 9:41

This will extract the rows from matrix A and B.


If you want to get them into a list (recommend)

single<-lapply(c(1:1000), function(x) matrix(c(A[x,],rep(B[x,],times=3)),nrow=2,byrow=T))

To put them all in diagonal matrix with adiag from magic package


for(i in 2:1000){

I could make it work without the loop (anyone any suggestions?)

  • $\begingroup$ Hi Jan and thank you for the answer. In the second command with the lapply, the function will take as argument the vector c(1:0000) right ? Moreover, after running this command single will be a list containing the 1000 matrices. So, if I need to extract lets say the first matrix, I will have to use single[[1]] ? (I am sorry I am not very familiar with lists) Lastly, do you know how this could be done with matrices ? My idea would be to create the 1000 matrices and then put them all in the diagonal of a huge matrix using the command "adiag". What do you think ? $\endgroup$
    – quant
    Aug 18 '16 at 10:05
  • $\begingroup$ Also now 21 is divided by 7. Your way in this case is really smart. But what if A had 20 rows ? $\endgroup$
    – quant
    Aug 18 '16 at 10:12
  • $\begingroup$ Yes, the access to the elements is exactly as you say. You probably could put them in one large matrix. I will have a look at it, never done it myself. And regarding the last comment, you mean if A had 20 columns? It would be up to you to make sure when you create the matrix, it has correct size. So if you had 41 elements, R would fill the missing element with the first element again - try matrix(c(1:41), nrow=2) $\endgroup$
    – Jan Sila
    Aug 18 '16 at 10:18
  • $\begingroup$ No I mean, that now you put in the first row the 21 column-elements of A and in the second row you put the 7 column-elements of B times 3 (so 3x7=21). that way the number of elements of each row match. But what if A had 20 columns ? Then you would need to put 2 times the column-elements of B and then put the rest 6 (so 2x7+6=20). Right ? Then I guess it should be done like matrix(c(A[x,],rep(B[x,],times=2),B[x,1:6]),...). Correct ? $\endgroup$
    – quant
    Aug 18 '16 at 10:23
  • $\begingroup$ yh that should work :) you can for example make a working example have two simple 3x3 matrices A and B and play around with the ideas and see what works. That will teach you the best (also I was checking on toy example if the code works)...regarding the adiag - it will make 42 x 21000 matrix, that is 6.7Mb worth of data..what you gonna do with it? :D $\endgroup$
    – Jan Sila
    Aug 18 '16 at 10:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.