some help with this small iteration procedure

I have a wave (that it´s a loop) and I would like to subtract the upper line points from the bottom line points. In fact, I´m interested only in certain range of my wave (this is why I choose the points i=3616 for the upper line and ii=16070 for the bottom line).
But I don´t know how to make the iterations! I mean the first value should be the point 3616 minus the point 16070, the next point should be 3615 minus 16071, and so on. The final point should be the last pair of values (that´s why I have put i>235, because for i=234 there is no more points in the wave.)
This points should be stored in a new wave with exactly the same number of points that iterations have been made.
I think that the problem in my procedure is in the definition of the for loop. As you can see, I don´t have any idea about programming, but I try to learn by myself :)


#pragma rtGlobals=3     // Use modern global access method and strict wave access.
Function/D iteration ()
Wave wave
variable i, ii
i=3616
ii=16070
make/O/N=??? new_wave
for (i=3616;i>235;i-=1)
    new_wave=wave[i]-wave[ii]
    i -=1
    ii+=1
endfor
end


Attached you´ll find the graph with the wave.
First, is you need to name the wave you are working on. The following is invalid.

Wave wave


Second, I've had difficulty in the past of getting a for loop to count down. I'll typically do something like

wave wave0
Make/o/n=3382 new_wave
variable i
for (i=235; i<3617; i+=1)
    newwave = wave0[3849-i] - wave0[15835+i]
enfor


I know that for loops should be able to count down, but I've personally never gotten it to work, so I count up and just modify the wave indexing.
proland wrote:
I know that for loops should be able to count down, but I've personally never gotten it to work, so I count up and just modify the wave indexing


Why?
for (i = 100; i >= 0; i-=1)
    // do something
endfor
741 wrote:

Why?
for (i = 100; i >= 0; i-=1)
    // do something
endfor


I don't know, when I try to run a decrementing for loop, it just never starts the loop, just as if you initialize the variable outside the end condition.
Attending to what you suggest, I try to run the procedure but it doesn´t make the iterations.

#pragma rtGlobals=3     // Use modern global access method and strict wave access.
Function/D cp2beta()
Wave X800__power
variable i
make/O/N=3850 '2cpbeta'
for (i=235;i<Numpnts('2cpbeta');i+=1)
    '2cpbeta'=X800__power[4419-i] - X800__power[15385+i]
endfor
end


It gives always the same value.
I did it manualy and the result given by the procedure is different to the value calculated manually.
daviddoji wrote:

#pragma rtGlobals=3     // Use modern global access method and strict wave access.
Function/D cp2beta()
Wave X800__power
variable i
make/O/N=3850 '2cpbeta'
for (i=235;i<Numpnts('2cpbeta');i+=1)
    '2cpbeta'[i]=X800__power[4419-i] - X800__power[15385+i]
endfor
end



you need to assign the value to a specific element of the wave '2cpbeta'. Sorry if i overlooked this in my quick example. The code you posted was assigning a single value to the entire wave. If your manual attempts and the code are giving different results, check the offset values for the waves (you have 4419 and 15385 : Make absolutely sure these are what you need).
Probably you need         '2cpbeta'[i-235]=X800__power[4419-i] - X800__power[15385+i].

But you can also use a simple wave assignment, something like this:
newwave = wave0[3849-p+235] - wave0[15835+p+235]
Note that the indexing is not efficient coding; I show the +235 in order to make the example clearer.

Read about wave assignment:
DisplayHelpTopic "Waveform Arithmetic and Assignment"

John Weeks
WaveMetrics, Inc.
support@wavemetrics.com
I might also point you to the Wave Arithmetic panel. To learn more about it look at the demo experiment: File->Example Experiments->Analysis->Wave Arithmetic Panel Demo. It provides a GUI for doing arithmetic like you want, handling complications of waves whose points don't align one-to-one, are offset, etc.

John Weeks
WaveMetrics, Inc.
support@wavemetrics.com