How to rearrange categoricals to a specified sequence

9 次查看(过去 30 天)
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 CenterFile Exchange 中查找有关 Data Distribution Plots 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by