Home vectorising multiple calls of Matlab 'find'

# vectorising multiple calls of Matlab 'find'

user8034
1#
user8034 Published in September 21, 2018, 8:04 am

I make a large number of calls to the 'find' function of Matlab. For example, the following should give the essence:

x=rand(1,10^8);
indx=zeros(1,10^8);
for i=1:10^8
indx(i) = find([0.2, 0.52, 0.76,1] < x(i), 1, 'last');
end


Is there a way to vectorize this code to speed it up? Just including x as a vector creates an error. If vectorization is not possible, then any other suggestions for speed would be appreciated. The actual problem I wish to solve has a considerably longer vector in the place of [0.2, 0.52, 0.76,1], so any solution shouldn't depend on the specific vector I provided.

thanks.

• With that example the result is always 4. Are you sure you want 'last'? – Luis Mendo Feb 14 at 0:54
• right....I've flipped the inequality (in my recent edit) to make it non-trivial. Just trying to construct a toy problem that captures the real issue in a simple way. Thanks for spotting it. – physioConfusio Feb 14 at 1:03
• Caveat: your loop will give error if the inequality doesn't satisfy for x. Try it with any value in x less than 0.2 – Sardar Usama Feb 14 at 4:44