-2

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

user155971
  • 99
  • 1
  • 6
  • Take a look here: [Using HIST and BAR to customize your histograms](http://blogs.mathworks.com/videos/2009/06/12/using-hist-and-bar-to-customize-your-histograms/). – Dev-iL Oct 18 '15 at 13:59
  • I saw it but not help , I try to use this way before but not working also – user155971 Oct 18 '15 at 14:14
  • What do you mean by "not working"? Does it result in an exception/error? What is the "_output desired_"? Please explain this because otherwise how can anybody help you...? Please upload an image/diagram/illustration of what is the result you're trying to get. – Dev-iL Oct 18 '15 at 14:16

1 Answers1

1

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

eyalsoreq
  • 358
  • 1
  • 9