MATLAB: Is there a way to obtain desired index without using ‘find’

findindexing

Dear all,
Suppose there is some array
ar=[102 243 453 768 897 ...]
Is there any way to obtain indices of an element having value e.g. 243 without using find?
For each element I need to find its index, so the computational cost will be O(N^2), which is too much. One possibility is using sparse matrices, e.g.
smatr=sparse(ar,1,1:length(ar))
and then index can be retrieved simply as ind=smatr(243) and so on, reducing computational cost to O(N). However, the problem is that in my computations values of 'ar' might exceed maximum size of matrix. So is there any additional way to relate values of 'ar' to indices so the latter can be obtained in one operation?
Thanks,
Dima

Best Answer

Here's another approach that uses the find_idx function from the FEX
It seems to overcome the O(N) overhead of HISTC and is virtually independent of N in speed,
ar=[102 243 453 768 897 102];
[sar,i,j]=unique(ar);
ind=j(floor(find_idx(768,sar))), %search for 768 for example