solution for sequentially concatenating matrices

10 ビュー (過去 30 日間)
Pablo
Pablo 2012 年 12 月 12 日
Hello,
I was wondering if there was a simple function to concatenate 2 matrices of same dimensions so that:
A=[ a c ]
[ b d ]
B=[ e g ]
[ f h ]
when concatenated it follows first column of A, then first column of B, second column of A, second column of B:
C=[a e c g]
[b f d h]
I can do it with a for loop, however I was wondering whether there was a more elegant way to do it?
Thanks
  2 件のコメント
Thomas
Thomas 2012 年 12 月 12 日
are you cat'ing string/numeric values? can you give an example with numbers?
Pablo
Pablo 2012 年 12 月 12 日
Yes, numbers are being concatenated, I think the solution below is the simplest one. However - I still need to place them within a cell matrix (that has column and row headers).
So for example --
D %cell matrix same size as A or B, where each cell will correspond to the merged values of C.
For example,
A=[ 1 3 ] [ 2 4 ]
B=[ 5 7 ] [ 6 8 ]
C=[1 5 3 7] [2 6 4 8]
And in the Cell matrix D: D {1,:}= column headers D{:,1}= row headers
and therefore: D {2,2} = [1 5] D{3,4} = [4 8]
Does this make sense?

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

採用された回答

Walter Roberson
Walter Roberson 2012 年 12 月 12 日
C = reshape(vertcat(A,B), size(A,1), []);

その他の回答 (1 件)

Pablo
Pablo 2012 年 12 月 12 日
Actually, I think the solution would be:
C(:,1:2:columns*2)=A; C(:,2:2:columns*2)=B;

カテゴリ

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