Approach #1
Vectorized solution with bsxfun
-
eqcond = bsxfun(@le,Targets.First_Time,sorted_set_of_times) & bsxfun(@ge,Targets.Last_Time,sorted_set_of_times);
[r,c] = find(eqcond);
for k =1:numel(sorted_set_of_times)
target_set = r(c==k);
end
Suggestion to use logical indexing (if applicable): If you are using target_set
to index into one of the dimensions of some variable named target
, which is of length same as Targets.First_Time
for that dimension, then instead of calculating target_set
you can directly index into that dimension of target
using eqcond(:,ind)
, where ind
would be the index to sorted_set_of_times
in that loop. Thus, as an example, if you were indexing into the rows of target
using target_set
in your original code like target(target_set,..)
, then you can do target(eqcond(:,ind),..)
instead.
Approach #2
Reduced data approach -
vind = find(Targets.First_Time <= Targets.Last_Time); %//Indices to reduce Targets datasize
Targets.First_Time = Targets.First_Time(vind);
Targets.Last_Time = Targets.Last_Time(vind);
for current_time = sorted_set_of_times;
target_set = vind([Targets.First_Time] <= current_time & [Targets.Last_Time] >= current_time);
end
Approach #3 (Hybrid of Approaches #1,2)
vind = find(Targets.First_Time <= Targets.Last_Time);
Targets.First_Time = Targets.First_Time(vind);
Targets.Last_Time = Targets.Last_Time(vind);
eqcond = bsxfun(@le,Targets.First_Time,sorted_set_of_times) & bsxfun(@ge,Targets.Last_Time,sorted_set_of_times);
[r,c] = find(eqcond);
for k =1:numel(sorted_set_of_times)
target_set = vind(r(c==k));
end
Suggestion to use logical indexing (if applicable): On a similar discussion as stated for approach #1, you could perform logical indexing for this one too. Thus, using the same notations as for that discussion, instead of doing target(target_set,..)
, you can do target(vind(eqcond(:,ind)),..)
.