how to append matrices of different sizes together

60 次查看(过去 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
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 CenterFile Exchange 中查找有关 Argument Definitions 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by