I have a question about gathering same numbers together in a matrix

1 次查看(过去 30 天)
Hello,
I have a matrix with 104x3 format.
In the third column I have values from 1 to 4. and the first 2 column I have different values. Let me give a quick and basic example:
15 20 1
45 30 1
12 10 1
48 78 3
18 16 4
20 56 3
13 14 1
17 85 3
14 55 2
33 6 4
45 7 4
and so on.
What I would like to do is to see the matrix in a form like, all the 1 values that are in the third column are one under the other, after that I want to see two's and then 3's and 4's like that:
x x 1
x x 1
x x 1
x x 2
x x 2
x x 3
x x 3
x x 3
x x 4
Is there a quick way to make this happen?
Thank you

回答(1 个)

the cyclist
the cyclist 2019-11-5
编辑:the cyclist 2019-11-5
If M is your matrix, then
M_sorted = sortrows(M,3)
will output M sorted by the 3rd column, maintaining the order of the other two columns. See the documentation for sortrows for details.

类别

Help CenterFile Exchange 中查找有关 Shifting and Sorting Matrices 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by