フィルターのクリア

how to combine matrix easier

1 回表示 (過去 30 日間)
metumetu
metumetu 2020 年 3 月 11 日
コメント済み: BobH 2020 年 3 月 12 日
Hello,
I want. How can i do this (below code block) with a loop?
I have to do this with a loop because, i will be doing this with a lots of a matrices. please help me.
Thank you.
a1=[1,2,3; 4,5,6; 7,8,9]
a2=[10,11,12; 13,14,15; 16,17,18]
a3=[19,20,21; 22,23,24; 25,26,27]
b1=[a1(:,1);a1(:,2);a1(:,3)]
b2=[a2(:,1);a2(:,2);a2(:,3)]
b3=[a3(:,1);a3(:,2);a3(:,3)]
C=[b1,b2,b3]
  2 件のコメント
BobH
BobH 2020 年 3 月 11 日
編集済み: BobH 2020 年 3 月 11 日
You are reading a1/a2/a3 in column order, all rows of col 1, then all rows of col 2, then all rows of col 3. This is the same as the single-index linear indexing
b1 = a1(:)
All the b1/b2/b3 assignments can be eliminated
C = [a1(:) a2(:) a3(:)]
Ameer Hamza
Ameer Hamza 2020 年 3 月 11 日
BobH, the actual problem is that OP wants to do it with lots of matrices, so manually concatenating array will not be practical.

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

採用された回答

Ameer Hamza
Ameer Hamza 2020 年 3 月 11 日
If the data is stored properly, i.e., in cell array instead of using separate variables, the following one-liner will work
a{1}=[1,2,3; 4,5,6; 7,8,9];
a{2}=[10,11,12; 13,14,15; 16,17,18];
a{3}=[19,20,21; 22,23,24; 25,26,27];
C = cell2mat(cellfun(@(x) x(:)', a, 'UniformOutput', 0)')';
  4 件のコメント
metumetu
metumetu 2020 年 3 月 11 日
Thank you so much for your help. Thats great for me
BobH
BobH 2020 年 3 月 12 日

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

その他の回答 (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