How can I plot histogram using b as x value and k as bin values? plotting histogram error line 16
Mostrar comentarios más antiguos
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 comentarios
rathod ujjval rameshbhai
el 1 de Sept. de 2015
i think k should be integer
Jackie
el 1 de Sept. de 2015
Jackie
el 1 de Sept. de 2015
dpb
el 1 de Sept. de 2015
nbins ~= integer doesn't cause the above error; it does change the binning calculation.
Need to attach your data to test to see what might be going on here.
Jackie
el 1 de Sept. de 2015
dpb
el 1 de Sept. de 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
el 2 de Sept. de 2015
Respuestas (1)
Image Analyst
el 2 de Sept. de 2015
0 votos
Why not just call histcounts() and then bar()?
Categorías
Más información sobre Histograms en Centro de ayuda y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!