How tp join multiple matrices in one matrix

조회 수: 14 (최근 30일)
Zhan
Zhan 2017년 5월 21일
답변: Star Strider 2017년 5월 21일
Assume matrix A1, A2 and A3 as follows:
A1 = [
1 10 23
2 12 45
3 23 43
4 32 45
5 32 32
];
A2 = [
1 43 10
2 56 11
3 34 24
4 43 65
];
A3 = [
1 32 12
2 22 21
];
I want to join all of these matrices into one matrix A according to the unique ID number (first column in every matrix). For example, all row with ID = 1 in all three matrices should written first, then all ID = 2, then all ID = 3, and so on.
A = [
1 10 23
1 43 10
1 32 12
2 12 45
2 56 11
2 22 21
3 23 43
3 34 24
4 32 45
4 43 65
];

채택된 답변

Star Strider
Star Strider 2017년 5월 21일
Use the sortrows function on the vertically concatenated matrices:
A = sortrows([A1; A2; A3], 1)
A =
1 10 23
1 43 10
1 32 12
2 12 45
2 56 11
2 22 21
3 23 43
3 34 24
4 32 45
4 43 65
5 32 32
Experiment to get the result you want. To eliminate the last row, the assignment becomes:
A = A(1:end-1,:);

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by