Effacer les filtres
Effacer les filtres

how to append matrices of different sizes together

29 vues (au cours des 30 derniers jours)
jaah navi
jaah navi le 10 Mar 2020
Modifié(e) : Ameer Hamza le 10 Mar 2020
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 commentaire
Ajay Kumar
Ajay Kumar le 10 Mar 2020
Does appending 0's or NaN's work?

Connectez-vous pour commenter.

Réponses (1)

Ameer Hamza
Ameer Hamza le 10 Mar 2020
Modifié(e) : Ameer Hamza le 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);

Catégories

En savoir plus sur Argument Definitions dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by