matlab中的直方图但不使用hist函数
histogram in matlab but without using hist function
我对如何在不使用 hist 函数的情况下在 MatLab 中绘制直方图有些困惑
问题是
生成一个介于 (0 ,100) 之间的随机数,并在 xy 上绘制这些随机数字的 1000 个,计划为直方图
例子
让间隔为 10
x | y
0 -10 | 5
10-20 | 9
20-30 | 15
等...
其中x是区间,y代表那个区间内重复的随机数
我试着写这段代码
function []=drawhist(a,b)
x=a+(b-a)*rand(1,1000);
bar(x)
end
但没有给我想要的输出,请帮助我了解如何编写此函数
这应该可以满足您的要求,但这是针对整数的。
如果您希望将其推广到浮点数,则需要定义采样精度并定义精度为一半的边缘
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
尽情享受
我对如何在不使用 hist 函数的情况下在 MatLab 中绘制直方图有些困惑
问题是
生成一个介于 (0 ,100) 之间的随机数,并在 xy 上绘制这些随机数字的 1000 个,计划为直方图
例子 让间隔为 10
x | y
0 -10 | 5
10-20 | 9
20-30 | 15
等...
其中x是区间,y代表那个区间内重复的随机数
我试着写这段代码
function []=drawhist(a,b)
x=a+(b-a)*rand(1,1000);
bar(x)
end
但没有给我想要的输出,请帮助我了解如何编写此函数
这应该可以满足您的要求,但这是针对整数的。 如果您希望将其推广到浮点数,则需要定义采样精度并定义精度为一半的边缘
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
尽情享受