Tuesday, September 17, 2013

Combining matrices by alternating columns

Combining matrices by alternating columns

I'm looking for a general approach to combine two matrices so that the
columns from the two initial matrices alternate in the new matrix
col1m1...col1m2...col2m1...col2m2...col3m1...col3m2......
for example:
matrix.odd=matrix(c(rep(1,3),rep(3,3),rep(5,3)),nrow=3,ncol=3)
matrix.even=matrix(c(rep(2,3),rep(4,3),rep(6,3)),nrow=3,ncol=3)
# would look like
matrix.combined=matrix(c(rep(1,3),rep(2,3),rep(3,3),rep(4,3),rep(5,3),rep(6,3)),nrow=3,ncol=6)
I'm looking for a general approach because I will have matrix combinations
with more than just 3 columns. I've tried some for loops and some if
statements but it isn't really coming together for me. Searches on
combining matrices with shuffle and with alternation have not proven
fruitful either. Any thoughts?

No comments:

Post a Comment