Hi. Suppose I have 100 numbers of (m*3) matrices. These matrices have different numbers of rows and same numbers of columns. I want to add them vertically and use the vertcat command in Matlab. How can I do that? Thanks for your help.

1 commentaire

Stephen23
Stephen23 le 5 Nov 2017
Modifié(e) : Stephen23 le 5 Nov 2017
Simply ensure that all of the matrices are stored in one cell array C, and then all you need to do is use a comma-separated list with vertcat:
M = vertcat(C{:})
MATLAB code can be so neat, simple, and efficient when people take care to design their data well!
Whatever you do, avoid magically creating/loading/acessing lots of separate variable names: this is a design decision that some beginners use to force themselves to write slow, complex, buggy, obfuscated code. Read more here:

Connectez-vous pour commenter.

 Réponse acceptée

Cedric
Cedric le 5 Nov 2017
Modifié(e) : Cedric le 5 Nov 2017

1 vote

If they are stored in cell array M:
Mvcat = vertcat( M{:} ) ;
where M{:} is what we call a comma separated list (CSL), equivalent to typing
M{1}, M{2}, ..., M{100}
in your case. Developing M as a CSL in the call to VERTCAT makes the call equivalent to (but much simpler!):
Mvcat = vertcat( M{1},M{2},M{3},M{4},M{5},M{6},M{7},M{8},M{9},M{10},M{11},M{12},M{13},M{14},M{15},M{16},M{17},M{18},M{19},M{20},M{21},M{22},M{23},M{24},M{25},M{26},M{27},M{28},M{29},M{30},M{31},M{32},M{33},M{34},M{35},M{36},M{37},M{38},M{39},M{40},M{41},M{42},M{43},M{44},M{45},M{46},M{47},M{48},M{49},M{50},M{51},M{52},M{53},M{54},M{55},M{56},M{57},M{58},M{59},M{60},M{61},M{62},M{63},M{64},M{65},M{66},M{67},M{68},M{69},M{70},M{71},M{72},M{73},M{74},M{75},M{76},M{77},M{78},M{79},M{80},M{81},M{82},M{83},M{84},M{85},M{86},M{87},M{88},M{89},M{90},M{91},M{92},M{93},M{94},M{95},M{96},M{97},M{98},M{99},M{100} ) ;

5 commentaires

Guillaume
Guillaume le 5 Nov 2017
And really to hammer the point being made by Cedric and Stephen. Your question has been answered, use vertcat{cellarrayofmatrices{:}) to vertically concatenate the hundreds of matrices that are store together in a cell array. If for some reason, they are not stored in a cell array but as individual variables, then you need to change your algorithm so that they are stored in a cell array in the first place. If you need help with that start another question.
mr mo
mr mo le 5 Nov 2017
I have 100 matrices were saved in a structure array and I want to vertically add them together. All of the matrices have same number of columns and different number of rows. How can I do that?
Stephen23
Stephen23 le 5 Nov 2017
Modifié(e) : Stephen23 le 5 Nov 2017
If the data are saved as one field of a non-scalar structure, then all you need to do is use a comma-separated list:
vertcat(yourstruct.fieldname)
If this does not work then you need to be a lot clearer about how the data are stored.
mr mo
mr mo le 5 Nov 2017
It works. Thank you very much.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Matrices and Arrays dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by