
Index x values of one wave using y values of another

bs
I would like to to set all of the y values in the rows of output whose nearest x values correspond to the y values of a second wave (of 50 rows; raster).
I would like to create a function and to avoid using a for loop because I will use this later in
Optimize
:variable i for(i=0; i<numpnts(raster)-1; i+=1) variable outputPnt = x2pnt(output, raster[i]) output[outputPnt] = 1 endfor
Igor's compiler isn't very smart- it will execute the numpnts() function every time through the loop.
Did you intend to terminate your loop one point shy of the end?
John Weeks
WaveMetrics, Inc.
support@wavemetrics.com
February 15, 2017 at 10:29 am - Permalink
I used the raster wave to make one calculation per data point in the wave, instead of the for loop. You could even MultiThread it if the Raster wave has enough data points for it to be worthwhile. I'm not sure what would happen if you try to MultiThread it and two points in Raster point to the same x-value in Output.
February 16, 2017 at 01:37 am - Permalink
Clever solution, but testing on my machine showed that it was actually almost twice as slow as the original implementation. Is that due to the wave lookup every time Test2 is called? I didn't try multithreading.
Another way I found to slow it down is to pre-fill a wave with the "x2pnt" calculation outside of the loop, then look up those values inside the loop. Curious. Here's my code:
Timing results:
•waveprep(); testA() //Original method
4706.18 microseconds
•waveprep(); testB() //Original with pre-filled x2pnt
7494.86 microseconds
•waveprep(); test1() //Olelytken method
8225.75 microseconds
I feel that there should be a way to have the pre-filled x2pnt wave (RasterPnts) set the indexed points in Output with one command, perhaps with MatrixOP waveIndexSet, but I couldn't figure out how.
February 16, 2017 at 02:30 pm - Permalink
x2pnt(output, raster[i])
is for some reason much faster than the single commandoutput[x2pnt(output, raster[i])] = 1
. I guess a conversion between different number types is taking place?I usually find that wave indexing is faster than a for loop and if I do a simple test like the one below that is also the case. The clean for loop executes in 1313 us and the wave indexing in 262 us. I'm not sure what the significant difference is between that and the above examples.
February 17, 2017 at 01:19 am - Permalink
Wow. This is very clever. Thanks.
February 21, 2017 at 07:34 am - Permalink