简体   繁体   English

matlab中的直方图但不使用 hist 函数

[英]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我有点困在如何在不使用 hist 函数的情况下在 MatLab 中绘制直方图

the question is that问题是

Generate a random number between (0 ,100) and plot 1000 of those random digits on xy,plan as histogram在 (0 ,100) 之间生成一个随机数并在 xy 上绘制 1000 个这些随机数字,计划为直方图

example let interval is 10示例让间隔为 10

x | × | y

0 -10 | 0 -10 | 5 5

10-20 | 10-20 | 9 9

20-30 | 20-30 | 15 15

etc ...等等 ...

where x is interval and y represent the repeated random number in that interval其中 x 是区间,y 表示该区间内重复的随机数

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享受

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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