Averaging waves which has NaN for some rows

Hi,
I need help.

I want to average waves which has NaN for some rows.
However, if i use the code "WavesAverage Example" in the Igor manual,
the destination wave also has NaN in the same rows.
Can I average waves ignoring the NaN rows ?

For example, please see the attached image, I want to make the averagedWave which is calculated average by ignoring the NaN rows.

I'm using Igor Pro 6.37 on Mac.

Please advise me.
Thanks in advance.

You can use
MatrixOP ReplaceNaNs(w, replacementVal)
to replace NaN values, but that is not what you want. I think you have to write your own procedure for it, along the lines of what I did below:


Function Test()
//	Test Function

	//	Creates three waves to average
	Make/O Wave1={1, 2, NaN, 4, 5}
	Make/O Wave2={3, 2, 1, NaN, -1}
	Make/O Wave3={2, 2, 2, NaN, 2}

	//	Creates a wave reference wave to hold the three waves
	Make/O/FREE/WAVE AllWaves={Wave1, Wave2, Wave3}
	
	//	Averages the waves
	Wave AverageWave=AverageAllWaves(AllWaves)
	
	//	Displays the result
	DoWindow/K AverageTable
	Edit/N=AverageTable Wave1, Wave2, Wave3, AverageWave
End


Function/WAVE AverageAllWaves(AllWaves)
//	Averages all waves ignoring NaNs
Wave/WAVE AllWaves

	//	Calculates the number of waves to average. The waves should all have the same length
	Variable n=NumPnts(AllWaves)
	
	//	Checks that at least one wave exists
	if (n>0)
	
		//	Uses the first wave in the list to create the waves needed for the calculation
		Wave SingleWave=AllWaves[0]
		Duplicate/O/FREE SingleWave, NumTypeWave, DivisionWave, SumWave
		Duplicate/O SingleWave, AverageWave
	
		NumTypeWave=0
		DivisionWave=0
		SumWave=0
		AverageWave=0
	
		//	Counts through the waves one at a time
		Variable i=0
		for (i=0; i<n; i+=1)
	
			//	The next wave to add to the total sum
			Wave SingleWave=AllWaves[i]
		
			//	Checks if any of the values are NaN
			NumTypeWave=(NumType(SingleWave[p])!=2)
		
			//	Adds the value to the total, unless the value is NaN in which case 0 is added
			SumWave+=(NumTypeWave[p]==1 ? SingleWave : 0)
		
			//	Increases the number to divide the sum with by one, unless the value is NaN in which case 0 is added
			DivisionWave+=NumTypeWave[p]
		endfor

		//	Divides the sum by the number of valid data points
		AverageWave=SumWave/DivisionWave
		
		//	Returns the average
		Return AverageWave
	endif
end
A less complex solution though relegated to 1D waves is to use wavetransform with zapnans to condition the wave you want an average from. Either creating a new wave or if acceptable overwriting the original.

If you are attempting to average across multiple waves on a point by point basis, this could be done with a loop creating a new wave from the points of the source waves and then doing a wavetransform with overwrite. Then do a mean(wavename).

Andy
We've already provided a solution for you with Igor 6.

From Igor's Analysis menu, choose Packages->Average Waves

Select the waves you want to average and click Do It.

NaNs will be accounted for. For example, if averaging 3 waves but only 2 have non-NaN values, the divisor of the average will be 2, not 3.

Pretty straightforward.

--Jim Prouty
Software Engineer, WaveMetrics, Inc.