How to rearrange categoricals to a specified sequence

5 ビュー (過去 30 日間)
Xaiver Sim
Xaiver Sim 2019 年 11 月 8 日
回答済み: Campion Loong 2019 年 11 月 15 日
Hi all,
I am a Earth and Environmental Science student trying to do his group project and failing miserably.
I am trying to plot the frequency of the number of storms per month for a year.
I used tabulate to get the total number of storms oer month and then plot them using bar.
However, the months that are tabulated are in a disorganised arrangement and i would like to plot them following the correct monthly order.
I have tried listing them out as per the matlab instructions however, it keeps returning me that it is not a permutation of the categorical.
the categorical gives me a 12x1 categorical and the names of the months are spelled correctly
I have also tried using the function perms to calculate all possible permutations of the 12 months, however , matlab does not let me as it is too big of an output (42.8GB)
Pls send help :)
2019-11-08 (1).png
untitled.png

回答 (1 件)

Campion Loong
Campion Loong 2019 年 11 月 15 日
The categories of your categorical array is sorted in alphabetical order.
% 'c' is your categorical array
>> categories(c)
ans =
12×1 cell array
{'April' }
{'August' }
{'December' }
{'February' }
{'January' }
{'July' }
{'June' }
{'March' }
{'May' }
{'November' }
{'October' }
{'September'}
Note that order of the categories is independent of the order of elements in the array itself:
>> c
ans =
12×1 categorical array
January
February
March
April
May
June
July
August
September
October
November
December
In this state, when you call bar with the categorical array and a numeric vector (i.e. your monthly rainfall), the visualization looks out of order:
% A naive example of 1:12
>> bar(c, 1:12)
untitled1.png
To get the bars in desired order, reorder the categorical array's categories before calling bar:
>> months_in_order = ["January" "February" "March" "April" "May" "June" "July" "August" "September" "October" "November" "December"];
>> c = reordercats(c, months_in_order);
>> bar(c, 1:12);
untitled1.png

カテゴリ

Help Center および File ExchangeDescriptive Statistics and Visualization についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by