Azzera filtri
Azzera filtri

Concatenate matrices into one matrix .... basic problem

1 visualizzazione (ultimi 30 giorni)
Giorgos
Giorgos il 7 Ago 2015
Modificato: Stephen23 il 7 Ago 2015
Hello Matlab,
Im new to Matlab and in general programming but im beginning to get the hang of it. I have a small question though...
say I have three column matrices say
A=[1 ;2 ;3 ;4 ;5];
B=[11 ;22 ;33 ;44 ;55 ;66];
C=[111 ;222 ;333 ;444 ;555 ;666];
How would I be able to incorporate all three matrices into one say:
D=[1 ;2 ;3 ;4 ;5 ;11 ;22 ;33 ;44 ;55 ;66 ;111 ;222 ;333 ;444 ;555 ;666]
Iv been trying to make loops for hours know with no success...
Thanks for any help :)

Risposte (1)

Stephen23
Stephen23 il 7 Ago 2015
Modificato: Stephen23 il 7 Ago 2015
You can read more in the [] and vertcat documentation:
D = [A;B;C]
or equivalently:
D = vertcat(A,B,C);
This is a good place to start to learn how to use MATLAB:
  1 Commento
Stephen23
Stephen23 il 7 Ago 2015
Modificato: Stephen23 il 7 Ago 2015
From your email: "I need to ask one last thing, say that instead of 3 matrices i have something like 20 of them and manually imputing them doesn't really work how would you again propose if manage this?"
The answer is simple: put all of those arrays in a cell array, and then it does not matter how many there are:
X = cell(1,3); % preallocate the final size
X{1} = [1 ;2 ;3 ;4 ;5];
X{2} = [11 ;22 ;33 ;44 ;55 ;66];
X{3} = [111 ;222 ;333 ;444 ;555 ;666];
D = vertcat(X{:});
Do not try to refer to variables using dynamic names, or using a loop with some evaluated strings. This is slow, buggy and unreliable. However storing the arrays in one cell array will be fast and simple code, suitable for any number of arrays.

Accedi per commentare.

Categorie

Scopri di più su Creating and Concatenating Matrices in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by