histogram of a m-by-n matrix

7 views (last 30 days)
Stefano
Stefano on 18 Jul 2014
Commented: Star Strider on 19 Jul 2014
I have a m-by-n matrix of values between 10^-20 and 10^-8 I drew it by
hist(My_mat(:),15);
I obtain only one big bin of the hist, instead I would like to get several of bins between min and max values. Is it correct my code above ? Do you have any suggestions?

Accepted Answer

Star Strider
Star Strider on 18 Jul 2014
I would do one of two approaches, depending on what your data distribution is:
  1. Increase the number of bins, perhaps from 25 to 100 depending on what you want to do;
  2. Take log10(X) and use that instead.
The first is probably the easiest. Experiment until you get the result you want.
  6 Comments
Stefano
Stefano on 19 Jul 2014
Ok, thank you so much! If I would to do the hist only of the nonzero values? The zero values maybe insignificant for my interest. I refer always to a matrix
Star Strider
Star Strider on 19 Jul 2014
My pleasure!
This should do what you want:
log10Data = -randi(12,1,25)-8;
[bcnts,ctrs] = hist(log10Data,15); % Create histogram counts
bcnts = bcnts(bcnts>0); % Eliminate counts with bcnts = 0
ctrs = ctrs(bcnts>0); % Eliminate bins with bcnts = 0
ydat = 10.^ctrs.*bcnts; % Create new data for calculations
L10YData = log10(ydat); % Log10 of new data
mnD = mean(ydat); % Mean of edited data
L10mnD = log10(mnD); % Log10 of mean
figure(1)
bar(ctrs, bcnts); % Plot histogram
set(gca,'XTickLabel',''); % Clear default labels
ypos = min(ylim) - diff(ylim)*0.05; % Set y-position for ‘text’
hxt = [min(L10YData) L10mnD max(L10YData)]; % Set ‘hxt’ to be [min mean max]
for k1 = 1:length(hxt)
xtklbl{k1} = sprintf('10^{%2.0f}\n%s',hxt(k1)); % Create x-tick labels
end
text(hxt, ones(1,length(hxt))*ypos, xtklbl, 'HorizontalAlignment', 'center')
It may look a bit strange because of the way I had to recalculate your data with the zero-count bins eliminated. That is done in the ‘ydat’, and creates a data vector with the values of the data at the bin centers weighted by the counts. This skews the data, but is the only way I could calculate it and make the data work for the plot, because with the zero-bins included, the xtick labels did not work correctly. The outcome is that the data plotted is not the data you started with.

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 18 Jul 2014
You should have 15 bins, though some of them might be virtually empty and too short to notice on the display. So do this:
counts = hist(My_mat(:), 15);
bar(counts, 'BarWidth', 1);
message = sprintf('You have %d bins', length(counts));
uiwait(helpdlg(message));
What does this say? So, if you do in fact actually have 15 bins then you might have to scale the data if you want to see all of the bins, like the others suggested. You can either scale the data values, or the counts - whatever it takes to see your data better.

Categories

Find more on Data Distribution Plots 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!