Combine two matrices (every other column)

I have two matrices A and B which are for example
A=[1 2 3;4 5 6; 7 8 9] and B=[10 11 12; 13 14 15; 16 17 18]
And I would like to combine these matrices so that every other column is from A and every other is from B. So the answer should be matrix:
[1 10 2 11 3 12; 4 13 5 14 6 15; 7 16 8 17 9 18]
Of course my matrices are not only 3x3 matrices but n x n matrices.
My history with Matlab is so short that I don't figure out if that is even possible to do?

 Réponse acceptée

Walter Roberson
Walter Roberson le 7 Fév 2016
Provided that A and B have the same number of rows and columns,
AB = reshape([A;B], size(A,1), []);

5 commentaires

Ke Xu
Ke Xu le 16 Oct 2017
this really helps me a lot. Thank you very much!! But I still do not understand why 'reshape' can automatically combine them every other column. That's perfect!
Suppose you have
A11 A12 and B11 B12
A21 A22 B21 B22
A31 A32 B31 B32
then [A;B] would be
A11 A12
A21 A22
A31 A32
B11 B12
B21 B22
B31 B32
MATLAB stores numbers "down" columns, so for example A21 is the entry immediately after A21, and B32 is the entry immediately after B22 . That applies for the [A;B] as well: the B11 is immediately after the A31. The order in memory for [A;B] is
A11 A21 A31 B11 B21 B31 A12 A22 A32 B12 B22 B32
Now reshape that to have 3 rows, knowing that it stores down columns:
A11 B11 A12 B12
A21 B21 A22 B22
A31 B31 A32 B32
this has exactly the same in-memory order: you have just changed the information about how to organize it into rows and columns.
Jan Siegmund
Jan Siegmund le 22 Mai 2020
should be the top answer
Ehsan Asadi
Ehsan Asadi le 27 Jan 2021
This is amazing. Can you please explain why and how it works? I am trying to understand it.
@Ehsan Asadi I already explained. It is a natural consequence of the fact that MATLAB stores values "down" columns, that for any matrix, the next item in memory after the first row first column is the second row first column then the third row first column, and so on, then then first row second column, second row second column, and so on. That, and the fact that reshape() does not change the order of items in memory and just changes how you refer to them.
A11 A12 A13
A21 A22 A23
is stored in memory as A11 A21 A12 A22 A13 A23 . You can reshape() it to be 3 x 2 and the order in memory would continue to be A11 A21 A12 A22 A13 A23 even though it would now appear to the user as
A11 A22
A21 A13
A12 A23
MATLAB stores (numeric) arrays as blocks of consecutive memory, and indexing is used to compute where to find where in the linear list to look. B(J,K) is found at linear location J + (K-1)*NumberOfRows
If you have used C or C++ you might be wanting to compare how they work. C and C++ store data "across the columns", so in the case of
A11 A12 A13
A21 A22 A23
the order in memory would be A11 A12 A13 A21 A22 A23. But in C or C++ it gets more complicated than that: it does have that way of storing memory but it has another more common way that can be difficult to tell apart.

Connectez-vous pour commenter.

Plus de réponses (2)

Snowfall
Snowfall le 9 Fév 2016

1 vote

Thank you for you both. These worked perfectly in my case.
Dillen.A
Dillen.A le 18 Fév 2019
I wanted to "weave" some colourmaps, then I came across this topic. In the end I solved it like this, since I wanted to do it a couple of times.
function out = weave(varargin)
% Example: weave(jet,cool,autumn,spring);
N = numel(varargin);
out = nan(N * max(cellfun(@(s) (size(s, 1)), varargin)), size(varargin{1}, 2));
for ii = 1:N
out(ii:N:size(varargin{ii}, 1) * N - (N - ii), :) = varargin{ii};
end
while all(isnan(out(end, :)))
out(end, :) = [];
end
end

4 commentaires

Stephen23
Stephen23 le 19 Fév 2019
Modifié(e) : Stephen23 le 19 Fév 2019
>> C = {[0,1,2;3,4,5],[6,7,8;9,10,11],[12,13,14;15,16,17]};
>> M = reshape(permute(cat(3,C{:}),[3,1,2]),[],3); % in one line
>> isequal(weave(C{:}),M)
ans = 1
Dillen.A
Dillen.A le 19 Fév 2019
Nice!
Only difference is when you have C={jet(8),cool(7)}; doesn't like it. but appending with nans would solve that.
Can we do the same for rows with three matrices to form in to one matrix with alternate rows of the three matrices?
Walter Roberson
Walter Roberson le 2 Avr 2020
Yes, this user weave function would be happy with any 2D matrices passed in, as long as the matrices all have the same number of columns.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Operators and Elementary Operations dans Centre d'aide et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by