Finding the x position of the Nth iteration of a given value in a wave
SurleyDoc
Hi.
I have a 1D numerical wave MyWave.
I’m trying to write a function that will return the row (Xpoint) at which a given value, XValue, can be found in MyWave for the Nth time.
For example, if MyWave = 0,1,0,1,1,1,1,0,1 and
If XValue = 1, and
If N = 1, the function should return xPoint = 2.
If N = 3, the function should return xPoint = 5.
Thanks for your help!
execute
DisplayHelpTopic "FindValue"
DisplayHelpTopic "FindLevels"
DisplayHelpTopic "FindDuplicates"
to learn about some operations that may be useful here.
FindDuplicates with the /INDX flag looks like a good solution.
January 23, 2022 at 03:17 am - Permalink
function FindNthValue(variable value, variable instance, wave w)
duplicate /free w w_x
w_x = x
extract /free/O w_x, w_x, w == value
if (numpnts(w_x) >= instance)
return w_x[instance-1]
endif
return NaN
end
You may want to check for values that match within some tolerance, rather than exact matches. Depending on how the data are created, you could run into trouble comparing, say, a single precision wave value with a double precision variable value.
January 23, 2022 at 03:40 am - Permalink
Note that tony's example code returns the point value in Igor's zero based counting system, so you need to add 1 to get the result your want (N = 1 gives result 2 etc.). Here is a slightly altered code which uses Extract in a different way and also returns 1-based results:
extract/free/indx w, occurence, w == value
if (instance > 0 && numpnts(occurence) >= instance)
return occurence[instance-1]+1
endif
return NaN
end
January 23, 2022 at 04:35 am - Permalink