automating sorting

Greetings forum,

I have a list R00 and want to sort its values. R00 contains positions that are spaced on average 10.

For example: values between 8 to 11 are stored in wave R1, values between 11 to 20 are stored in wave R2 , values between 20 to 30 are stored in wave R3 and so on,

Right now I manually sort for each interval as following:


#pragma rtGlobals=1     // Use modern global access method.

function Bin_v2()

Wave R00

Duplicate R00, copy
        MatrixOP/O index = col(copy,0)
        index = index[p] > 8 && index[p] < 11 ? p : NaN
        Wavetransform zapNaNs index
        Make/O/N=(numpnts(index)) R1 = copy[index[p]]
        MatrixOP/O index2 = col(copy,0)
        index2 = index2[p] > 11 && index2[p] < 20 ? p : NaN
        Wavetransform zapNaNs index2
        Make/O/N=(numpnts(index2)) R2 = copy[index2[p]]
        MatrixOP/O index3 = col(copy,0)
        index3 = index3[p] > 20 && index3[p] < 30 ? p : NaN
        Wavetransform zapNaNs index3
        Make/O/N=(numpnts(index3)) R3 = copy[index3[p]]
        MatrixOP/O index4 = col(copy,0)
        index4 = index4[p] >30 && index4[p] <40 ? p : NaN
        Wavetransform zapNaNs index4
        Make/O/N=(numpnts(index4)) R4 = copy[index4[p]]
        MatrixOP/O index5 = col(copy,0)
        index5 = index5[p] >40 && index5[p] <50 ? p : NaN
        Wavetransform zapNaNs index5
        Make/O/N=(numpnts(index5)) R5 = copy[index5[p]]
        MatrixOP/O index6 = col(copy,0)
        index6 = index6[p] >50 && index6[p] <60 ? p : NaN
        Wavetransform zapNaNs index6
        Make/O/N=(numpnts(index6)) R6 = copy[index6[p]]

can you suggest a possible loop that could do this?

Thank you

Without looking at your code in detail, this looks like a situation where running the built-in Extract procedure could do the trick, possibly followed by calling Sort on each of the resultant waves R1,R2,..


If the original wave is multi-dimensional, you might want to use Extract's /indx flag and then write your own code to deal with the columns 

In reply to by gsb

Thanks for reply,

Lets assume it's one dimensional (not multi). I changed my code and all it does just sorts into intervals. Now I don't wanna manually do it for each R, is there a way to do a loop?

Try something like:

Function Bin_v2(R00)
    Wave R00      
    MatrixOP/O index = col(R00,0)
    Make/FREE rangeW={8,11,20,30,40,50,60}
    Variable i,v1,v2
    String name
        MatrixOP/O index1=replace((greater(index,v1)*greater(v2,index))*indexRows(index),0,NaN)
        Wavetransform zapNaNs index1
        Make/O/N=(numpnts(index1)) $name=R00[index1[p]]


In the event that R00 is multi-dimensional you can modify the last make command, e.g.,

Make/O/N=(numpnts(index1),numCols) $name=R00[index1[p]][q]

I hope this helps,



In reply to by Igor


Is replace , greater and indexRows are waves? because right now i get  syntax error (indexRows) , if i compile it as it is.

If they are wave and i make them separately , I get error: Nans are not allowed in this operation line 11

The code is for IP 8.x where Replace() greater() etc are MatrixOP functions.

IP6 is way too old. 

Obviously you can rewrite

         MatrixOP/O index1=replace((greater(index,v1)*greater(v2,index))*indexRows(index),0,NaN)

In IP6 code using code similar to:

Duplicate/O index,index1
index1 = index[p] > v1 && index[p] < v2 ? p : NaN