How to combine 2 matrices (or vectors) element by element in order (with different sizes)?

4 ビュー (過去 30 日間)
How can I combine 2 matrices A and B into one so that the new matrix C = element 1 of A, followed by element 1 of B, then element 2 of A, element 2 of B, etc? Note that vectors A and B are randomly generated and the size might be different.
For example, A = [x1 x2 x3 ...], B = [y1 y2 y3 ...] and then C=[x1 y1 x2 y2 x3 y3 ...]
Thank you.

採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2016 年 8 月 14 日
編集済み: Azzi Abdelmalek 2016 年 8 月 14 日
A=1:3
B=4:6
C=[A;B]
C=C(:)'
%or
C=reshape([A;B],1,[])
  2 件のコメント
kadaldu
kadaldu 2016 年 8 月 14 日
Thank you Azzi so much for your help and quick response. this (C=reshape([A;B],1,[])) works perfectly.
Thanks again, Khalid
Csanad Levente Balogh
Csanad Levente Balogh 2021 年 1 月 11 日
Hi! I'm looking for a simmilar thing. How would this vork with more than two vectors arranged in a matrix? For example:
a = [1 2 3];
b = [4 5 6];
c = [7 8 9];
d = [10 11 12];
And it should result in:
res =
1 4 2 5 3 6
7 10 8 11 9 12
Which is:
res =
a(1) b(1) a(2) b(2) a(3) b(3)
c(1) d(1) c(2) d(2) c(3) d(3)

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by