How to compare counts of each histogram
Show older comments
Hello everyone, i hope you are doing well. i have the eight clusters. I want to calculate histogram for each cluster, after calculating histogram, i want to compare the count of each values if the count is greater in one cluster assign the value to other cluster and repeat the process till 8 clusters
How can i do that in MATLAB
I have the following code.
for i = 1:8
T = clusters{i}(:,2);
h1(i)=histogram(T,100000,'BinLimitsMode','manual','BinLimits',[0 3e8]);
end
7 Comments
Image Analyst
on 14 Jun 2022
Explain in much, much more detail what "compare" means to you. What exactly do you want to see or compute? It doesn't make sense to just use max() to set the counts equal to the max counts of any histogram, at least not to me.
Stephen john
on 14 Jun 2022
Your explanation is still not clear to me. Let's take a smaller problem. Here's code that creates a fixed sample data set with 100 elements. Walk us through exactly what steps you want to follow and what results you want to receive with this data set and let's say 4 clusters.
rng default
x = randi(10, 1, 100);
histogram(x)
One possible guess at what you're trying to do is to partition the data into bins that are as close to equal in size as you can get. If that were the case, I don't think you need histogram or histcounts. Using prctile or sorting the data and taking the elements that are evenly spaced (in the case above with 100 values and 4 bins, use elements [25 50 75] of the sorted data.
figure
s = sort(x);
xline(s([25 50 75]), 'r')
hold on
histogram(x, 'BinEdges', [0 s([25 50 75]), 11])
Stephen john
on 14 Jun 2022
Rik
on 14 Jun 2022
Your description does not seem to make sense. If you make a histogram, how can one value in your original data contribute to multiple bins?
If a person is aged 47, they will only contribute to the count of people aged between 45 and 50, not to the count of people aged 50 to 55.
It therefore seems you aren't using the terminology I'm expecting. It looks like Steven Lord and Image Analyst have a similar problem.
Try explaining again, but this time with data (at least a tiny example). Maybe you don't want a histogram at all, but you want to group values. That would make some more sense to me.
Stephen john
on 14 Jun 2022
Stephen john
on 15 Jun 2022
Edited: Stephen john
on 15 Jun 2022
Answers (1)
Saksham Gupta
on 14 Jun 2022
As per my understanding, you are unable to find the count value of histograms whose data is present with you.
‘Values’ attribute helps us to get bin count in histogram in MATLAB
Following code will help you in getting a cell array having count values of each Histogram:
clusters=num2cell(load('matlab.mat'));
h1= cell(1,8);
for i = 1:8
T = clusters{1,1}.clusters{i,1}(:,2);
z =histogram(T,100000,'BinLimitsMode','manual','BinLimits',[0 3e8]);
disp(z.Values)
h1{i}=z.Values;
end
You may also refer to the following documentation:
7 Comments
Stephen john
on 14 Jun 2022
Image Analyst
on 14 Jun 2022
Did you overlook my comment above? Again, define "compare" since taking the max in each bin, as you seem to say, makes little sense.
If you have any more questions, then attach your data and code to read it in with the paperclip icon after you read this:
Stephen john
on 14 Jun 2022
Steven Lord
on 14 Jun 2022
In a histogram in MATLAB, you can't have overlapping bins. If the second element of your data is 10 and that element is included in bin number 3, then all the other elements containing 10 in the data (be they element 1, 3, 42, or 1000000) must be in bin number 3. So your "same value exist[sic] in other cluster" can't happen if your "cluster" is a histogram bin.
Start at the beginning. Tell us what your data represents, explain exactly what you do to your data at each step as though we have no idea what you're doing. You may assume we know how to write MATLAB code but you may not assume we know your application.
Stephen john
on 14 Jun 2022
And the explanation???
s = load('matlab.mat')
clusters = s.clusters % a cell array
for k = 1 : numel(clusters)
subplot(3, 3, k);
histogram(clusters{k});
grid on;
ylabel('Count');
xlabel('Value');
end
What do the 5 columns, and variable number of rows represent?
Stephen john
on 14 Jun 2022
Categories
Find more on Descriptive Statistics 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!

