finding the starting points of the steps in a staircase like function
10 次查看(过去 30 天)
显示 更早的评论
Hi
I will appreciate help on this problem:
I generate a vector C from a set of weighted samples (you can see in the figure that the below code will generate). C looks like a staircase with the number of steps equal to length(targets). I would like to find that index (from the bottom of C) of C where a stable step begins.
I came up with a simple logic that has been commented in the code. If you run the code after un-commenting it, the required indices will will stored in 'indx'. But this does not work all the time. I am sure there is an elegant way of doing this. I will appreciate support on this.
clear all; clc; close all;
% Some specifications
xmin=1; xmax=40; % state space
R=.5; % observation variance
targets=[9.2,28.35]; % locations of the objects of interest
N=100; % no. of samples
% Generate the sorted samples
particles=xmin+(xmax-xmin)*rand(1,N);
particles=sort(particles, 'ascend');
% Find the weight of the samples
for i=1:length(targets)
z(i)=targets(i)+sqrt(R)*randn;
exPo(i,:)=exp(-(particles-z(i)).^2/2/R);
end
w=(1/sqrt(2*pi*R))*sum(exPo);
% Generate C
C(1)=w(1);
tmp=0; cc=1;
for i=2:length(w)
if w(i)>=w(i-1)
C(i)=tmp+w(i);
else
C(i)=C(i-1);
tmp=C(i-1);
end
% if w(i)<w(i-1)
% if C(i)==C(i-1) & C(i)>C(i-2)
% indx(cc)=i-1;
% cc=cc+1;
% end
% end
end
% Plotting
figure(1); subplot(211);
stem(particles,w,'m.'); hold on;
axis([xmin,xmax,0,1]); grid on;
title('Weighted samples');
figure(1); subplot(212);
stem(C, 'm.'); grid on;
title('The function C');
0 个评论
回答(1 个)
Jan
2013-8-1
编辑:Jan
2013-8-1
Do you mean:
indx = find(diff(C))
after the loop?
Please note, that "does not work all the time" does not contain enough information to understand in which cases the results differ from your expectations. Posting an explicit example would be more useful. Or do you get an error message e.g. when "i-2" creates a zero index?
Formatting the code properly is a good idea, when you want others to read it. Follow the "? Help" link to learn the details. You are not a newcomer in this forum anymore and I cannot understand, why you still do not care for a good readability.
When you omit the ugly "clear all", you could use the debugger to see, what's going on in your code. Set some (conditional) breakpoints in the code to check, why the results do not match your expectations.
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!