필터 지우기
필터 지우기

Concatenate matrices into one matrix .... basic problem

조회 수: 1 (최근 30일)
Giorgos
Giorgos 2015년 8월 7일
편집: Stephen23 2015년 8월 7일
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에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by