Concatenate matrices into one matrix .... basic problem

1 次查看(过去 30 天)
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 :)

回答(1 个)

Stephen23
Stephen23 2015-8-7
编辑:Stephen23 2015-8-7
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 个评论
Stephen23
Stephen23 2015-8-7
编辑:Stephen23 2015-8-7
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by