how to append matrices of different sizes together
29 ビュー (過去 30 日間)
古いコメントを表示
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 件)
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);
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Argument Definitions についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!