MATLAB: Fast way to perform multiple searches on a large array

findmatrices

I have a large time series array (10,000,000 elements) :
ts = [2; 1; 3; 4; 6; 7; .......]
I have a corresponding time array (same size as the above) :
times = [d1; d2; d3; d4; d5.......]
I have 2 arrays of start times and end times (also large ~ 30000 elements):
st = [dd1 dd2 dd3 ....]
en = [de1 de2 de3 ....]
I need to create a new matrix with many many finds. Logic is :
results = NaN(300, numel(st));
for i=1:numel(st);
temp = ts(find(times > st(i) & times < en(i) , 300,'first');
results(:,i) = temp;
end;
Is there any ay I do this faster (ideally without a loop) ?
  • I have a 64 bit version so I can try a large in-memory solution.
Many thanks in advance, Nigel

Best Answer

I think by dumping the past times you might be able to speed up the find. If st(i+1) > en(i), then you could dump even more elements, but I think the savings will be small. This code relies on times, st, and en being sorted.
results = NaN(300, numel(st));
offset = 0;
for i=1:numel(st);
idx = find(times > st(i), 1,'first');
offset = offset+idx-1;
times = times(idx:end);
results(:,i) = ts(0:299+idx+offset);
end