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.
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
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
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
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.
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
col_interleave = reshape([a'(:) b'(:)]',2*size(a',1), [])'
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
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
reshape([A ; B], size(A, 1), [])