Matlab 如何累积直方图?

Matlab 如何累积直方图?,matlab,histogram,matlab-figure,Matlab,Histogram,Matlab Figure,我的数据范围从0到1e5(以微秒为单位)。我想为连续的2.68微秒制作直方图,为0.05微秒进行装箱,最后累加所有直方图。我有以下尝试: a=load('Data.dat') Range=2.68; % For every successive 2.68micro seconds, I want to plot histogram n= max(a)/Range; % This tells how many such ranges will be in the data low=min(a);

我的数据范围从0到1e5(以微秒为单位)。我想为连续的2.68微秒制作直方图,为0.05微秒进行装箱,最后累加所有直方图。我有以下尝试:

a=load('Data.dat')
Range=2.68; % For every successive 2.68micro seconds, I want to plot histogram
n= max(a)/Range; % This tells how many such ranges will be in the data
low=min(a);
high=min(a)+Range;
x=low:0.05:high;
y=hist(a,x);% This is the first histogram for the first 2.68 microsecond
figure
semilogy(x,y,'-ob')

for num=1:1:n-1  % This loop is to make histogram for successive 2.68 microseconds
low=low+Range;
high=high+Range;
x=low:0.05:high;
y=y+hist(a,x-low); % This (I AM NOT SURE) is intended to accumulate the histogram for each  loop. 
end

figure
semilogy(x-low,y,'-or'); % Final accumulated histogram.

这是我制作的节目。但在循环的每次运行之前,直方图似乎并没有积累起来。有人能帮我把柱状图累加起来吗?或者其他更好的方法来积累连续范围的柱状图?

你做得不对。有几件事你做得不对:

首先,没有正确计算出一张照片的数量。要知道你需要做的事情的数量,你需要数据的频率

freq=0.05; %seeing your comments
n=numel(a)*freq/Range;
nindexRange=Range/freq;   %how many data is in each Range
这将为您提供要计算的件数。但是,请注意,这通常不是整数。我们也需要考虑到这一点

lastpiece=numel(a)-floor(n)*nindexRange; 
% This gives us how many elements are we leaving for the last calculation
因此,让我们把n设为整数

n=floor(n); %round it to the lowest
现在,您只需要获取一段数据,并计算相同数量的值的直方图。假设您想要一个包含10个箱子的直方图

nbins=10;
bins=linspace(min(a),max(a),nbins);
现在我们已经定义了直方图所依赖的
a
的值。让我们循环

for ii=1:n
   y=y+hist( a(1 + (ii-1)*nindexRange : nindexRange*ii) , bins);  
end
那么我们在那里做什么? 我们每次迭代只取一块
a
,总是得到相同箱子的柱状图(否则累加就没有意义了)。那么我们如何获取数据呢?a
(1+(ii-1)*nindexRange:nindexRange*ii)
试着整理一下,应该很难;)

但是哦!我们忘了我们遗漏了一些数据!(因为n不是整数!)

所以

if laspiece~=0
  y=y+hist(a(end-laspiece:end),bins); % take the last "lastpeice" amount of points
end

这应该能奏效

我不知道你想做什么<代码>历史(a,x-low)对于循环的每次迭代都将保持不变,对吗?您正在使用完全相同的数字调用函数time@AnderBiguri:当我最终累积直方图时,我使用x-low来拟合x轴。如您所见,每次循环后,x值向前移动2.68微秒。为了实现自上而下(累积),应在每个循环后调整x值。我想知道的是“每连续2.68微秒发生了什么?”所以连续2.8微秒的直方图需要累加。@AnderBiguri:我不确定是否有其他更好的方法在每次跑步后累加直方图。如果有别的办法,请告诉我。谢谢。
x-low
每次迭代都是相同的值吗?我的意思是
x=low:0.05:high
->
x-low=low:0.05:high-low
->
x-low=0:0.05:high-low
high-low
范围
,因此是常数。因此,
hist
的输入总是
a
0:0.05:Range
。要更正代码,我需要知道
a
的用法。它们是每秒钟取多少微秒的值?采样频率是多少?我不明白为什么
n=max(a)/范围。
的值是否为`微秒?如果是的话,你想做什么?谢谢。但有两个问题:1)最终直方图不是累积直方图!直方图似乎是组合在一起以返回原始数据!我认为问题发生在y=y+hist(a(1+(ii-1)*nindexRange:nindexRange*ii),bins)。在这里,不是一个一个地积累,而是一个一个地连续装箱!最终直方图预计是所有直方图(连续“范围”中获得的直方图)的累积2)最后一个“如果循环”中的错误:试图访问a(5057.71);索引必须是正整数或逻辑。1)它确实在累积直方图。
hist
的结果是每个定义的bin中的数据量。我不知道你不明白什么。你确定你想要的是直方图吗?2) 的确,函数中有一个输入错误,但现在应该可以工作了