How tp join multiple matrices in one matrix

8 ビュー (過去 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 Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by