how to append matrices of different sizes together

67 ビュー (過去 30 日間)
jaah navi
jaah navi 2020 年 3 月 10 日
編集済み: Ameer Hamza 2020 年 3 月 10 日
I am having three different matrices as
A = [1 2; 3 4]
B = [4 5 6; 7 8 9]
C=[10 11 12 13; 14 15 16 17]
i want to have the result in the following manner
D=[ 1 2;
3 4;
4 5 6;
7 8 9;
10 11 12 13;
14 15 16 17]
could anyone please help me on it.
  1 件のコメント
Ajay Kumar
Ajay Kumar 2020 年 3 月 10 日
Does appending 0's or NaN's work?

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

回答 (1 件)

Ameer Hamza
Ameer Hamza 2020 年 3 月 10 日
編集済み: Ameer Hamza 2020 年 3 月 10 日
In MATLAB, all the rows and columns of the matrix must be of equal length. If you just want to combine them in one variable, I suitable way is to use cell Array;
A = [1 2; 3 4]
B = [4 5 6; 7 8 9]
C= [10 11 12 13; 14 15 16 17]
D = {A,B,C}
The other option is to replace the absent entries with NaN
A = [1 2; 3 4];
B = [4 5 6; 7 8 9];
C = [10 11 12 13; 14 15 16 17];
D = {A,B,C};
mat_sizes = cellfun(@(x) size(x), D, 'UniformOutput', 0)';
max_dims = max(cell2mat(mat_sizes), [], 1);
result = cellfun(@(x,y) ...
padarray(x, [0, max_dims(2)-size(x,2)], 'post'), ...
D', mat_sizes, 'UniformOutput', 0);
result = cell2mat(result);

カテゴリ

Help Center および File ExchangeArgument Definitions についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by