how to append matrices of different sizes together
29 views (last 30 days)
Show older comments
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.
Answers (1)
Ameer Hamza
on 10 Mar 2020
Edited: Ameer Hamza
on 10 Mar 2020
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 Comments
See Also
Categories
Find more on Argument Definitions in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!