Sort categorical variables in an alphabetical order

1 vue (au cours des 30 derniers jours)
alpedhuez
alpedhuez le 12 Déc 2020
Commenté : alpedhuez le 12 Déc 2020
I have a set of categorical variables
A = {'NY','Boston'}
How can one sort A to B as
B = {'Boston','NY'}
I tried sort but does not work.
  2 commentaires
Ive J
Ive J le 12 Déc 2020
This is very sruprising that sort doesn't work for you (I'm wondering how is this even possible).
A = {'NY', 'Boston'};
sort(A)
1×2 cell array
{'Boston'} {'NY'}
alpedhuez
alpedhuez le 12 Déc 2020
I think in general a better pratice to define a proxy variable (Boston = 1 and NY =2) and work on the proxy variable instead of dealing with letters.

Connectez-vous pour commenter.

Réponse acceptée

Rik
Rik le 12 Déc 2020
As Ive Ive suggested: the sort function will do what you need:
B = categorical({'NY','Boston'});
sort(B)
ans = 1×2 categorical array
Boston NY

Plus de réponses (0)

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