Assigning a returned wave from a function without looping

Hello there,

I made a simple function that returns a sub-wave of the input wave. Reason for making it a function is to loop it over multiple 2D images and not create a mess in the experiment. Here is how it looks in use:

Make/N=(75, 35)/O test2d
test2d = CreateSubwave2D_func(imageWave, 9.4, 10, 4, 5)[p][q]
// The function simply returns a sub-wave of the input 2D wave with x-range from 9.4 to 10 and y-range for 4 to 5.

The problem is that during assignment to "test2d", the function is invoked for every pair of p and q, running the function 75*35=2625 times just to create one sub-wave (I think this is what's happening). Is there a way to assign the whole returning wave to "test2d" without looping?

Another issue demonstrated in the snippet above is that I have to create a wave with predetermined dimensions to assign the returning wave to. Is there a way to create a flexibly-dimensioned wave that adheres to the returning wave? Something like this:

Make/N=(10, 10) test2d
test2d = CreateSubWave2D_func(imageWave, 9.4, 10, 4, 5)
// the function returns a 75x35 wave. During assignment, test2d re-dimensions to match the returning wave's dimensions.

I can bypass all of this by using a macro instead, but as stated earlier it tends to create a lot of residue waves in the experiment which I have to kill manually, not being ideal for dealing with lots of data. An advice would be appreciated, thank you and happy Friday!

Do you really need to copy the returned wave? Why not something like

Wave tempw = CreateSubwave2D_func(...)
... do something with temp2 ...

If really do need the copy, then do it in two lines:

Make/N=(10, 10) test2d
Wave tempw = CreateSubWave2D_func(imageWave, 9.4, 10, 4, 5)
test2d = tempw[p][q]

 

I wonder if you can replace your function entirely by using the range parameters of the Duplicate operation?

make/O/N=(100,100) twod=p*q
setscale x, 0,0.25,"", twod
setscale y, 0,0.25,"", twod
newimage twod
duplicate/O/R=(0.1, 0.15)(0,0.1) twod, subset
newimage subset

This creates a wave named subset the is just the portion of the twod wave over the x range of 0.1 to 0.15, and the y range of 0 to 0.1.

Consider also using the /FREE flag in Duplicate or Make operations to create waves that will automatically go away when there are no longer any references to them.