Interleave matrices in MATLAB with reshape

If you have two matrices, arrays or vectors that you want to interleave row by row or column by column in MATLAB, you can accomplish this with a reshape command. This tutorial shows how to do this. Since vectors are just one-dimensional matrices, this technique will also work for them.

Interleaving two matrices by row

Suppose you have two matrices of the same size, A and B, with the following elements:

Matrix A:

A1A3
A2A4

Matrix B:

B1B3
B2B4

Interleaving the two matrices row by row produces the following combined matrix:

A1A3
B1B3
A2A4
B2B4

To do this, you can use the following matlab code:

% create two matrices.
a = [11 13; 12 14];
b = [21 23; 22 24];
 
% interleaves two same sized matrices by row
row_interleave = reshape([a(:) b(:)]',2*size(a,1), [])
% Note that the reshape requires that a and b be the same size.

The resulting matrix from MATLAB:

row_interleave =

    11    13
    21    23
    12    14
    22    24

Interleaving two matrices by column

You again have two matrices, A and B with the following entries:

Matrix A:

A1A3
A2A4

Matrix B:

B1B3
B2B4

Interleaving the two matrices column by column produces the following combined matrix:

A1B1A3B3
A2B2A4B4

The code is very similar to interleaving by row, except you transpose matrices A and B prior to the interleaving process, and then transpose the resulting reshaped matrix:

% create two matrices.
a = [11 13; 12 14]
b = [21 23; 22 24]
 
% interlave two same sized matrices by column
a = a';
b = b';
col_interleave = reshape([a(:) b(:)]',2*size(a,1), [])'
% Note that the reshape requires that a and b be the same size.

The resulting combined matrix looks like this:

col_interleave =

    11    21    13    23
    12    22    14    24

How reshape combines the matrices

To explain why the above operations work, it is necessary to understand how MATLAB internally indexes arrays. Each array element, no matter if it is a 1D, 2D or multi-dimensional array, can be accessed by a 1D index number. All arrays are stored in a specific way, with element indexes having a specific order. Take a look at this console output:

>> a = [1 3 5; 2 4 6]

a =

     1     3     5
     2     4     6
     

You can access individual elements in the array:

>> a(2)

ans =

     2
     

And you can make a 1D array out of a 2D array by “flattening” it:

>> a_ = a(:)

a_ =

     1
     2
     3
     4
     5
     6

a_ = a(:) creates a 1D vector representation of a, with elements appearing in the index order. The elements will appear in the internal MATLAB indexing order, which stores each column contiguously one after another. MATLAB indexes elements along the first dimension of the matrix (which is rows) until it reaches the end of the first dimension (bottom row) and then loops back to the top row but at a new column. This indexing order continues until reaching the end of the array.

The reshape command restructures the matrix to a different size but the order of the indices of the individual elements is unchanged. Thus, when you reshape, the elements will still appear in the same order but are just “re-flowed” to the new shape of the matrix:

>> reshape(a, 3, 2)

ans =

     1     4
     2     5
     3     6

>> reshape(a, 1, 6)

ans =

     1     2     3     4     5     6

>> reshape(a, 6, 1)

ans =

     1
     2
     3
     4
     5
     6

To interlace two matrices together, a new array is explicitly constructed which puts the elements from the original matrices into the necessary order. Then, reshape is used to restructure this new matrix to the desired output matrix.

Discussion

Ruben, 2011/02/21 18:29
Nice tutorial! Please notice that interleaving the columns of two non-square matrices requires the reshape parameter to be "2*size(a,2)" instead of "2*size(a,1)".
Peter Yu, 2011/02/21 20:46
Hmm... I tried it with non-square matrices just now and the original code still seemed to produce the right answer. There might have been some subtle differences between your code and mine though - I too got a case where I had to use "2*size(a,2)" instead of the original "2*size(a,1)". I even updated the page but then noticed it was due to a very small typo in the code. I love the way small things affects results in MATLAB! :)
Ruben, 2011/02/22 16:49
You're right, your code works fine as it is. My mistake!
I wrote a function based on your code but introduced a slight modification that in the end required me to use "2*size(a,2)". When interleaving columnwise I don't explicitly transpose the input matrices a and b before calling reshape() as you do. Therefore I use the "number of columns of original matrix a" instead of "number of rows of transposed matrix a" as a parameter for the reshape() function.
As you say, small change, big difference. :)
Regards,
R.
ahmad hasan khan, 2011/08/07 06:20
Goog
jay, 2012/02/06 12:07
for cols interleaving:
if you have an matrix A with with size(A) = [rows,cols] :

if cols>rows
you have to transpose the matrices for this to work
Peter Yu, 2012/02/14 02:55
Jay - I already included the transpose in the code above for the column interleaving case.
Friend, 2014/07/04 09:44
Thank you :)
Stephen, 2014/08/11 09:17
Thank you for your nice tutorial! For Octave users, just be careful, if you want to inline the transposes in the column interleaving code, don't forget it inside the size():

col_interleave = reshape([a'(:) b'(:)]',2*size(a',1), [])'
I would love to hear your feedback. Enter your comment below [ Terms of Use ]:
THYSQ
 

About Peter Yu I am a research and development professional with expertise in the areas of image processing, remote sensing and computer vision. I received BASc and MASc degrees in Systems Design Engineering at the University of Waterloo. My working experience covers industries ranging from district energy to medical imaging to cinematic visual effects. I like to dabble in 3D artwork, I enjoy cycling recreationally and I am interested in sustainable technology. More about me...

Feel free to contact me with any questions about this site at [user]@[host] where [user]=web and [host]=peteryu.ca

Copyright © 1997 - 2014 Peter Yu