I have 2 waves of data. Z_EC is proton number, N_EC is neutron number, and EC_pure is a flagged wave based on certain perimeters, IE flag "1" if meets criteria, "0" if not.
I'm trying make a new proton and neutron wave that will have the correct values based on whether or not EC_pure is a 1 or 0 (If 1 match P to N; if 0 do nothing). However, every time I run my function, the new waves fill in the P and N values even when EC_pure = 0
Function plotECzn()
wave EC_pure, Z_EC, N_EC
variable i, j
variable len = numpnts(Z_EC)
Duplicate/O Z_EC Z_pure
Duplicate/O Z_EC N_pure
For(i=0; i<len; i+=1)
For(j=0; j<121; j+=1)
If(EC_pure[j] == 1)
Z_pure[i] = Z_EC[i]
N_pure[i] = N_EC[i]
Endif
Endfor
Endfor
End
A more compact way to write this is:
Function ListECzn()
WAVE EC_pure, Z_EC, N_EC
variable Len = NumPnts(EC_pure)
Make/O/D/N=(Len) Z_pure, N_pure
Z_pure[] = (EC_pure[p] == 1) ? Z_EC[p] : NaN
N_pure[] = (EC_pure[p] == 1) ? N_EC[p] : NaN
WaveTransform zapnans Z_pure
WaveTransform zapnans N_pure
End
These conditional assignments are really useful. The syntax of Z_pure[] = (EC_pure[p] == 1)? Z_EC[p]: NaN
Z_pure[] = (EC_pure[p] == 1)? Z_EC[p]: NaN
means: assign the following to each row of Z_pure - if EC_Pure is 1 then assign the Z_EC value, if not, assign NaN. Then you just delete the NaNs. Done.
Another possibility would be to duplicate the waves and assign NaNs if EC_Pure == 0 and then zapnans.
Function ListECzn()
WAVE EC_pure, Z_EC, N_EC
wavestats/Q EC_pure
variable N_Candidates = V_Sum
Make/O/D/N=(N_Candidates) Z_pure, N_pure
variable Len = NumPnts(EC_pure)
variable i
variable j = 0
For(i=0; i<Len; i+=1)
If(EC_pure[i] == 1)
Z_pure[j] = Z_EC[i]
N_pure[j] = N_EC[i]
j += 1
EndIf
EndFor
End
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.