How can I plot histogram using b as x value and k as bin values? plotting histogram error line 16

1 view (last 30 days)
load sizes2.txt
results= sizes2(:,1)
n=size(sizes2,1) % num of rows
a=sizes2(:,:)
b=sort(a)
y=log10(n)
k= 1+3.322*y % is suggested number of class intervals and n is the number of values in the data set
fprintf('The suggested number of class intervals is %d\n', k );
x=range(b)
w=x/k % width of class intervals
fprintf('The suggested number of class intervals is %d\n', w)
k=8
hist(b,k)
Thank you
  7 Comments
dpb
dpb on 1 Sep 2015
I can't reproduce an error; I would simplify quite a lot, but I get no error...I did just paste the data from the text file into the command window as variable sizes. Whatever is causing the above error is somewhere else than in the above.
>> n=length(sizes);
>> k=1+3.322*log10(n) % without rounding
k =
8.3130
>> k1=fix(k); % truncate; use round() if to nearest integer
>> n=hist(sizes,k) % with the non-integer value
n =
58 51 27 11 4 4 2 2
>> m=hist(sizes,k1) % and the integer version...
m =
58 56 22 14 2 3 2 2
>> sum(m)==sum(n) % both get all the elements
ans =
1
>> hist(sort(sizes),k1) % and to so you don't need sort()
>> o=hist(sort(sizes),k1)
o =
58 56 22 14 2 3 2 2
>>
I didn't delve into the code of hist to see just exactly what it's doing with the non-integer bins; clearly it also truncated to the integer number but it did modify the binning as can be seen comparing the two outputs.
But, it doesn't cause an indexing error (and it doesn't appear to be the problem in your case as you still got an error)
Oh; you haven't by chance inadvertently created a variable hist, have you? Try
clear hist
and then try again...or, to see before clear try
which hist
Jackie
Jackie on 2 Sep 2015
Hi, thank you so much for the clarification actually yeah I think at some point when I was trying to put this together I accidentally at some point put hist=... and later realized that this can't be done . Once I put clear hist the historgram was plotting as desired. Thank you again

Sign in to comment.

Answers (1)

Image Analyst
Image Analyst on 2 Sep 2015
Why not just call histcounts() and then bar()?

Categories

Find more on Data Type Identification in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

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

Start Hunting!