Sort categorical variables in an alphabetical order

조회 수: 6 (최근 30일)
alpedhuez
alpedhuez 2020년 12월 12일
댓글: alpedhuez 2020년 12월 12일
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
Ive J
Ive J 2020년 12월 12일
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 2020년 12월 12일
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.

댓글을 달려면 로그인하십시오.

채택된 답변

Rik
Rik 2020년 12월 12일
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

추가 답변 (0개)

카테고리

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