简体   繁体   中英

histogram in matlab but without using hist function

I'm a little bit stuck on how to plot a histogram in MatLab without using hist function

the question is that

Generate a random number between (0 ,100) and plot 1000 of those random digits on xy,plan as histogram

example let interval is 10

x | y

0 -10 | 5

10-20 | 9

20-30 | 15

etc ...

where x is interval and y represent the repeated random number in that interval

I try to write this code

function []=drawhist(a,b)

x=a+(b-a)*rand(1,1000);

bar(x)

end

but not give me the output desired , please help me with any idea to understand how to write this function

This should do what you want, however this is for integers. If you want this to generalise to flots you need to define the accuracy of sampling and define edges that are half that accuracy

function [centers,freq] = drawhist(range,interval,density)
% example 
% generate 1000 random integers ranging between 0 and 100;
% drawhist([0,100],10,1000);
V = randi([0,100],density,1); 
min_x = range(1); max_x = range(2); 
bin = linspace(min_x,max_x,interval+1);
freq = zeros(interval,1);
for ii=1:interval
   freq(ii) = sum(V>bin(ii)&V<bin(ii+1));
end
centers = bin(2:end)-(bin(2:end)-bin(1:end-1))/2;
bar(centers,freq);

end

Enjoy

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM