How to efficiently identify if integer is in wave?
ilavsky
Hello,
I have wave of integers (16 bit, random order positive integers) and I need to know if a specific integer value is already in that wave.
First I wrote function which iterates over the wave, but I nearly felt ashamed when I saw the result.
Then I used extract for points which match that integer value and checked the extracted wave length. Still, had to write user function for that.
I still feel there could be some single line solution to this simple issue which is escaping me. Any suggestions?
Thanks a lot!
Have you tried FindValue?
FindValue /I=myVal myWave
March 31, 2021 at 10:05 am - Permalink
In the event that your wave contains more than one instance of the integer value (say i_value) you can write in IP9 the following command:
MatrixOP/o/p=1 aa=zapNaNs(replace(equal(myWave,i_value)*IndexRows(myWave),0,nan))
For example:
•MatrixOP/o/p=1 aa=zapNaNs(replace(equal(ddd,58)*IndexRows(ddd),0,nan))
aa={3,17,43,57,83,97,123} // index points where 58 is found.
In the case of SP/DP data one needs to take into account some tolerance value. Here is an example:
ddd+=enoise(0.1)
Variable tol=0.15
Variable value=58
MatrixOP/o/p=1 aa=zapNaNs(replace(greater(tol,abs(ddd-value))*IndexRows(ddd),0,nan))
aa={3,17,43,57,83,97,123}
March 31, 2021 at 10:21 am - Permalink
Great, thanks!
FindValue somehow escaped me, not sure why... And I suspected MatrixOp should be able to help. I need to carefully evaluate that expression. Both solve my problem better than my original solution.
March 31, 2021 at 12:30 pm - Permalink
Don't forget that in IP8 and later, FindValue has a /UOFV flag which will do the search for the value in multiple threads. If you only care if you have a given value in the wave at least one time, this flag might be appropriate if you're not already running the code in threads.
March 31, 2021 at 01:38 pm - Permalink