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

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