How to vertcat 100 matrices ?

2 views (last 30 days)
mr mo
mr mo on 5 Nov 2017
Commented: mr mo on 5 Nov 2017
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 Comment
Stephen23
Stephen23 on 5 Nov 2017
Edited: Stephen23 on 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:

Sign in to comment.

Accepted Answer

Cedric
Cedric on 5 Nov 2017
Edited: Cedric on 5 Nov 2017
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 Comments
Stephen23
Stephen23 on 5 Nov 2017
Edited: Stephen23 on 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 on 5 Nov 2017
It works. Thank you very much.

Sign in to comment.

More Answers (0)

Categories

Find more on Matrices and Arrays 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!