function for categorizing numerical array by size
古いコメントを表示
So I have an array of numbers, and I am trying to find the occurences of numbers <10,000, occurences of numbers between 10,000 and 50,000, and occurences of numbers of values between 50,000 to 100,000. I am then trying to group these three values into a pie chart. Basically its for a project where I need to show the percentage of people who pay below 10,000, percentage of people who pay between 10,000 and 50,000, and so on.
I know there are numerous way to do this but I think I am just overwhelmed and can't figure out one to go with?
Thanks for help
4 件のコメント
Tommy
2020 年 4 月 24 日
Would something like this work for you?
% sample array
A = 100000*rand(1000, 1);
% find and plot percentages
freq = sum([A<10000,A>=10000&A<50000,A>=50000])./numel(A);
pie(freq, {'X < $10,000', '$10,000 <= X < $50,000', 'X >= $100,000'});
Tommy
2020 年 4 月 24 日
It should work. There are ways to get it to work with your data in the table, also, to avoid converting to a cell array and then string array if possible. But how is your data stored in the table? As numbers or as text?
Tony
2020 年 4 月 24 日
Tommy
2020 年 4 月 24 日
Fantastic, happy to help!
回答 (1 件)
Steven Lord
2020 年 4 月 24 日
0 投票
Use histcounts.
The histogram function is similar but creates a histogram plot. histcounts just does the binning (no plot) and returns counts which you could then pass into pie or other functions.
カテゴリ
ヘルプ センター および File Exchange で Logical についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!