how to sort the elements of 2D matrix and understand their initial column and rows of each elements?
10 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
zohre saeedi
le 10 Juil 2016
Réponse apportée : Alexander Venus
le 19 Fév 2019
I have 20*30 matrix . I want to ascending sort matrix and understand their initial column and rows of each elements. for example a=[7 10 6;8 6 11] then sort b=[6 6 7 8 10 11] ,initial index of 10 is (1 2),.... can any body help me to write matlab code?
0 commentaires
Réponse acceptée
Stephen23
le 10 Juil 2016
Perhaps you want something like this:
>> a = [7,10,6;8,6,11]
a =
7 10 6
8 6 11
>> [R,C] = ndgrid(1:size(a,1),1:size(a,2));
>> [b,idx] = sort(a(:));
>> b % sorted values:
b =
6
6
7
8
10
11
>> [R(idx),C(idx)] % initial (R,C) indices of the sorted values
ans =
2 2
1 3
1 1
2 1
1 2
2 3
0 commentaires
Plus de réponses (1)
Alexander Venus
le 19 Fév 2019
this can also be accomplished more intuitive by using ind2sub:
>> a = [7,10,6;8,6,11]
a =
7 10 6
8 6 11
>> [b,idx] = sort(a(:));
>> b
b =
6
6
7
8
10
11
>> [i, j] = ind2sub(size(a), idx)
i =
2
1
1
2
1
2
j =
2
3
1
1
2
3
0 commentaires
Voir également
Catégories
En savoir plus sur Shifting and Sorting Matrices dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!