Efficient searching to find the first element of an array meeting a condition
Show older comments
Hello,
If a vector is given and the task is to find the first element fulfilling a condition then we use the
command 'finb(...,1)'. However, imagine that checking whether any member of my vector meets
the condition is going to be computationally expensive. In such a case the linear search is not a good idea.
An example:
f = @(x) rand(x);
find(f(1:10)>0.5);
Note that in this example it does not take so much time to check the 'condition' (i.e., wether it is bigger than 0.5) but in my actuall problem it is really expensive. So, my question is this: is there an efficient way to find the first element of an array fulfilling a condition?
Thanks in advance!
Babak
Accepted Answer
More Answers (3)
Bruno Luong
on 24 Aug 2023
Edited: Bruno Luong
on 24 Aug 2023
just fo the obvious for loop
for i = 1:length(x)
if expensive_check_for_meet_condition(x(i))
ifind = i;
break
end
end
for-loop the most fundamental statement in any programming language often neglected by MATLAB users
6 Comments
Mohammad Shojaei Arani
on 24 Aug 2023
Florian Bidaud
on 24 Aug 2023
It is actually probably the fastest in many cases indeed !
x=rand(1,1e7);
tic
is1 = find(atan(x)<1e-3,1); % here you need to evaute 1e7 test condition
toc
tic
for i=1:length(x)
if atan(x(i))<1e-3 % here you stop as soon as you find the first one that meet the criteria
is2=i;
break,
end
end
toc
is1 == is2
Mohammad Shojaei Arani
on 24 Aug 2023
Bruno Luong
on 24 Aug 2023
Edited: Bruno Luong
on 24 Aug 2023
If you know how to sort the array so that the condition meet becomes stronger from start to the end, meaning the expensive_check_for_meet_condition(x) always returns 0s followed by the 1s, a dichotomy (binary) strategy search can be adopted. You cut in half the array check the middle, depending on the test value, you skip the left or right side until your interval reduces to 1 element.
Mohammad Shojaei Arani
on 25 Aug 2023
"find" will use the "first" option by default which means that the command will return the first occurence of the event and will not search further.
4 Comments
Mohammad Shojaei Arani
on 24 Aug 2023
Torsten
on 24 Aug 2023
I don't know the search strategy of "find". Look up the MATLAB documentation and possible references for the find-command.
Florian Bidaud
on 24 Aug 2023
Whislt it's true, the find function will still have to deal with the whole array, consuming unnecessary time if the condition is met early.
Mohammad Shojaei Arani
on 24 Aug 2023
C. Rithiya
on 6 Sep 2023
0 votes
a = f(1:12); tic find(a>0.5,1) toc ans = 1 Elapsed time is 0.425812 seconds. tic p = 2; % 1/Probability i = 0; while true b = a(i*p+1:(i+1)*p); I = find(b>0.5,1); if ~isempty(I)>0 break end i = i+1; end value = i*p+I toc value = 1 Elapsed time is 0.027613 seconds.
Categories
Find more on Startup and Shutdown in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!