using histogram in a loop with hold on and off

I am tyring to understand the use of the histogram in the following way.
I have a matrix of size (M x N). Each column represents a curve.
I calculate a mean for all the curves using mean_matrix = mean(matrix,2)
Plotting mean_matrix shows that the mean is as expected.
Next I subtract each curve from the mean value giving me a difference matrix Diff(M x N)
I then use histogram to plot Diff(M x N) so it is > hist(Diff, # of bins)
This give me a histogram with different colours, I think the different colours
are for each column of the Diff matrix ? I then took a look at using
a loop so I did >for i=1,index; hist(Diff(:,i),# of bins);end; and this
gave me a different looking graph; next I included hold on and again
I got a completely looking curve. Can someone help me understand what I did?
I would like to do is to look at the histogram curves in a 3D plot
and see the variation better.

Answers (2)

I try to understand and to explain. Let's take a matrix, maybe the peaks-matrix (49x49 by default) and reshape it:
matrix=peaks; %49x49
matrix2=reshape(matrix,numel(matrix)/7,7); %343x7
hist(matrix2) %histogram with 7 bars each bin, one for each column of the matrix
figure, axes, hold all
for cnt=1:7
hist(matrix2(:,cnt))
end %7 histograms stacked upon each other -> difficult to see anything
h3=histc(matrix2,linspace(minv,maxv,21),1); %calculate a histogram for each column
bar3(h3) %plot the histogram for each column
Hope that helped,
best regards,
Michael

1 Comment

Michael, thanks for the feedback. Can you help me understand the use of the
linspace. I extracted out the bin counts and the bin values using the following
for n=1:index;
[bincounts,bins] = hist(Diff(:,index),No_bins);
bin_count = transpose(bincounts);
bin_count_t(:,n) = bin_count;
bin_value = transpose(bins);
end
The matrix bin_count_t (M x N) now contains the number of occurance of the
difference between the mean and each curve. bin_value is (M x 1) and
contains the values of each bin.
I then do a bar (bin_value, bin_count_t) again the stacking occurs as you
noted. Now since I already have the bin count and bin values extracted
how do I use linspace to separate out the curves ?, I experimented with
bar3 but not making progress, thanks inadvance for you feedback if you have time.
Regards
Philip

Sign in to comment.

I see I forgot to post also the line where minv,maxv is created:
minv=min(matrix2(:));maxv=max(matrix2(:));
What happens, if you execute the above written just using your Diff matrix instead of my matrix2?

Asked:

on 24 Jul 2014

Answered:

on 25 Jul 2014

Community Treasure Hunt

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

Start Hunting!