histweight.m

来自「关于粒子滤波(PF)的matlab程序」· M 代码 · 共 35 行

M
35
字号
function [plotx,ploty]=histweight(x,w,N,edges)
%makes a histogram with N bins for the distribution of x, weighted with w.

[x,I]=sort(x);
w=w(I);

if(nargin<4)
binwidth=(x(end)-x(1))/N;
centers=linspace(x(1),x(end),N);
lim=linspace(x(1)-binwidth/2,x(end)+binwidth/2,N+1);
else
    binwidth=(edges(2)-edges(1))/N;
centers=linspace(edges(1),edges(2),N);
lim=linspace(edges(1)-binwidth/2,edges(2)+binwidth/2,N+1);
lim(end)=x(end);%to prevent out of bounds on the next few rows
end
y=zeros(1,N);

k=2;%points at the upper bin limit
for i=1:length(x)
    if(x(i)>lim(k))
        %ok, we must jump to the next bin
        k=k+1;
    else
        %we are still in the right bin
    end
        y(k-1)=y(k-1)+w(i); %add the weight to the bin               
end

%scale everything so that it is a real pdf
area=sum(y)*binwidth;
plotx=x;
ploty=y;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?