Concatenate matrices into one matrix .... basic problem
1 view (last 30 days)
Show older comments
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 :)
0 Comments
Answers (1)
Stephen23
on 7 Aug 2015
Edited: Stephen23
on 7 Aug 2015
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 Comment
Stephen23
on 7 Aug 2015
Edited: Stephen23
on 7 Aug 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.
See Also
Categories
Find more on Logical 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!