Concatenate matrices into one matrix .... basic problem
조회 수: 2 (최근 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 :)
댓글 수: 0
답변 (1개)
Stephen23
2015년 8월 7일
편집: Stephen23
2015년 8월 7일
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
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 Center 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!