function for categorizing numerical array by size
Show older comments
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 Comments
Tommy
on 24 Apr 2020
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
on 24 Apr 2020
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
on 24 Apr 2020
Tommy
on 24 Apr 2020
Fantastic, happy to help!
Answers (1)
Steven Lord
on 24 Apr 2020
0 votes
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.
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!