# 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:

 A1 A3 A2 A4

Matrix B:

 B1 B3 B2 B4

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

 A1 A3 B1 B3 A2 A4 B2 B4

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:

 A1 A3 A2 A4

Matrix B:

 B1 B3 B2 B4

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

 A1 B1 A3 B3 A2 B2 A4 B4

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

, 2011/02/21 13: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)".
, 2011/02/21 15: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! :)
, 2011/02/22 11: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.
, 2011/08/07 02:20
Goog
, 2012/02/06 07: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
, 2012/02/13 21:55
Jay - I already included the transpose in the code above for the column interleaving case.
, 2014/07/04 05:44
Thank you :)
, 2014/08/11 05: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), [])'
, 2015/12/20 21:25
let's say i have 2 matrices; A=[A1 A2 A3 A4; A5 A6 A7 A8] ; B=[B1 B2 B3 B4; B5 B6 B7 B8] and C=[C1 C2 C3 C4; C5 C6 C7 C8]. I want to combine A,B,C and make a new matrix D which should look like following:

D= [A1 A2 B1 B2 C1 C2 A3 A4 B3 B4 C3 C4; A5 A6 B5 B6 C5 C6 A7 A8 B7 B8 C7 C8]

Can you please tell me how to do it ? I will also appreciate if you can show me also how to do it by using for loop. (I am a new matlab programmer , please excuse me if the request is too silly......). Thanks
, 2017/06/19 08:39
Thank you very much for the nice tutorial. After understanding and given a second though, I could figure out a simpler way to interleave columns:

A
w x y z
w x y z
w x y z

B
o p q r
o p q r
o p q r

[A ; B]
w x y z
w x y z
w x y z
o p q r
o p q r
o p q r

reshape([A ; B], [], size(A, 1))
w o x p y q z r
w o x p y q z r
w o x p y q z r
, 2017/06/19 08:41
It seems I didn't think enough :(

reshape([A ; B], size(A, 1), [])
, 2018/05/09 23:10
Hi! THanks for writing this. Finally get it.
Cheerz!