igor help file for calculation of median

I was going through "analysis.ihf" in the igor help. IN the following function

Function/D Median(w, x1, x2) // Returns median value of wave w
Wave w
Variable x1, x2 // range of interest

Variable result

Duplicate/R=(x1,x2) w, tempMedianWave // Make a clone of wave
Sort tempMedianWave, tempMedianWave // Sort clone
SetScale/P x 0,1,tempMedianWave
result = tempMedianWave((numpnts(tempMedianWave)-1)/2)
KillWaves tempMedianWave

return result
End

why do we need to scale tempMedianWave.I guess it is already scaled while duplicating. I cudnot find find any difference if i do not use SetScale.
Please help me to understand.

Vinod
The scale command explicitly sets the scaling from the arbitrary scaling of the incoming wave to start = 0, delta = 1. This is needed for the operation in the next line (which extracts the exact center of the wave by interpolation); the sort command itself doesn't need scaling.
That there is no difference for you without this command most definitely owns to the fact that you probably already start with a wave 'w' using the default scaling, which is 0,1. Try to scale your test wave to, say, start = 53, delta = 0.13 and see if you can still live without the extra scaling.
Thanks for the explanation. Changing the scale of the original data wave helped me to understand.
I wanted to extend this program to calculate random percentiles(say 95th). At first it was not clear to me, how IGOR calculates 95th percentile? For a dataset attached below i tried to calculate 75th percentile by two ways
1 using analysis>statistics>calculate percentile
2. using StatsQuantiles
both the methods give different values for 75th percentile.
I wrote a function to calculate percentile which gives similar result that i get from StatsQuantiles.

Vinod Kumar
IISER Mohali
percentile_calc.pxp
I think the Wave Percentiles panel, which was written a few years before StatsQuantiles was written (and by two different programmers) uses a slightly different definition of non-median quantiles. Here is some code and comments from procedure file that implements the panel:
            // the following corresponds to Q3 on http://mathworld.wolfram.com/Quantile.html
           
            // This combined with Igor's auto interpolation results in the standard definition of the median:
            //      if N is even, average the two middle values; if N is odd, take the middle value.
//          percentileCutoff = TmpPercentiles[j]*nReps - 0.5
//          // for percentiles other than 0.5 (the median) we simply pick the value whose sequence number is
//          // closest to N*quantile. This also guarantees that the 1 quantile (100th percentile) takes the last good number,
//          // not the next number past the last good number.
//          if (TmpPercentiles[j] != 0.5)
//              percentileCutoff = floor(percentileCutoff)
//              if (percentileCutoff > nReps-1)
//                  percentileCutoff = nReps - 1
//              endif
//          endif

            // This is the formula advocated by NIST on http://www.itl.nist.gov/div898/handbook/prc/section2/prc252.htm
            // It corresponds to Q6 on the URL above.
            percentileCutoff = TmpPercentiles[j]*(nReps+1) - 1
            if (percentileCutoff < 0)
                percentileCutoff = 0
            elseif (percentileCutoff > nReps-1)
                percentileCutoff = nReps-1
            endif
           
            // According to NIST, Excel uses this which corresponds to Q7 on the mathworld web site.
//          percentileCutoff = TmpPercentiles[j]*(nReps-1)
//          if (percentileCutoff < 0)
//              percentileCutoff = 0
//          elseif (percentileCutoff > nReps-1)
//              percentileCutoff = nReps-1
//          endif

There is generally agreement that the median for an even number of points is the average of the two data points on either side of the middle. But there are several different definitions for quantiles other than the median. Take a look at the links above- they are authoritative.

John Weeks
WaveMetrics, Inc.
support@wavemetrics.com