|
"Bernard.Mangay" <fcmisc@googlemail.com> wrote in message <81648ce9-1206-427f-8947-8342b33412bc@j2g2000vbo.googlegroups.com>...
> I have a monotonically increasing series.
>
> For each value in the series, I want to find the index of the next
> value that is atleast a specified amount amount above it.
>
> My code for it is below, but is slow due to too many loops. However,
> I can't work out how to do it faster. Could anyone please help?
>
>
> dn = cumsum((1/86400) * rand(100000,1));
>
> seconds_10 = 10/86400;
>
> dn2 = dn+seconds_10;
>
> I = nan(length(dn),1);
>
> tic;
> for i=1:length(dn)
>
> dn2i=dn2(i);
>
> for j=i:length(dn)
> if dn(j)>dn2i
> I(i) = j;
> break;
> end
> end
>
> end
> toc;
This should work:
for ii=1:length(dn) %ii since i is sqrt(-1)
test_vec = dn(ii:end)>dn2(ii);
if any(test_vec)
I(ii) = find(test_vec,1,'first');
end
end
%Sean
|