Effacer les filtres
Effacer les filtres

How to sort one field of a structure based on the values of another field?

2 vues (au cours des 30 derniers jours)
Rinu
Rinu le 12 Mar 2016
Commenté : Rinu le 12 Mar 2016
Let's say I have a structure 'struct' with two fields 'field1' and 'field2'. The length of the fields is unknown, but could be maximum 6. Each element of 'field1' must be a letter from the set {a,b,c,d,e,f} with no repetitions of a letter. Is there any way I can sort the values of 'field2' according to 'field1' in alphabetical order. For example, if 'field1' = [d b f] and 'field2' = [4 9 7], I would like to sort 'field2' such that 'field2' = [9 4 7].

Réponse acceptée

Chad Greene
Chad Greene le 12 Mar 2016
Modifié(e) : Chad Greene le 12 Mar 2016
The sort function returns the order of indices of an array for sorting. So
[A_sorted,ind] = sort(A);
can be used to sort another array B by
B_sorted_by_A = B(ind);

Plus de réponses (0)

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!

Translated by