How can I plot histogram using b as x value and k as bin values? plotting histogram error line 16
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
dpb
2015-9-1
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
回答(1 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Distribution Plots 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!