Sorting array by second column to first

12 vues (au cours des 30 derniers jours)
First Last
First Last le 1 Fév 2022
Modifié(e) : First Last le 1 Fév 2022
Hello Guys,
i have a problem to sort my array.
Its an unsorted array, which rows need to be sortet.
The value of the first row in the second column, needs to be the first value of the second row in the first column.
Example:
unsortet:
2 3
1 2
4 1
6 7
3 6
7 10
sortet:
1 2
2 3
3 6
6 7
7 10
10....
I would realy appreciate it, if you can help me :)

Réponses (1)

Benjamin Thompson
Benjamin Thompson le 1 Fév 2022
Here is one way to do matrix sorting. Your description is a little confusing, but hopefully from this you see the general approach using the sort function.
>> A = [2 3; 1 2; 4 1; 6 7; 3 6; 7 10]
A =
2 3
1 2
4 1
6 7
3 6
7 10
>> [B, I] = sort(A(:,1))
B =
1
2
3
4
6
7
I =
2
1
5
3
4
6
>> Asorted = A(I,:)
Asorted =
1 2
2 3
3 6
4 1
6 7
7 10

Catégories

En savoir plus sur Shifting and Sorting Matrices dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by