How to use IDL’s min() function’s min_subscript argument in for loop?

I have experience in python but am new to IDL. I am trying to write a function that will return two bins. I want to use the min function to get my bin edges. My issue is that I am trying to use the min_subscript argument to denote each bin edge, and I can’t figure out how to do this in a for loop. I want to write my code so that each loop has 2 different min_subscript variables (the two edges of the bin), and these variables are written into their own arrays. Here is my code:

FUNCTION DBIN, radius, data, wbin, radbin, databin
    FOR i = 0, N_ELEMENTS(radius)-1 DO BEGIN
        l = lonarr(N_ELEMENTS(radius))
        m = lonarr(N_ELEMENTS(radius))
        junk1 = min(abs(radius - radius[i]), l[i])
        junk2 = min(abs(radius - (radius[i] + wbin)), m[i])
        radbin = lonarr(N_ELEMENTS(radius))
        radbin[i] = radius[l[i]:m[i]]
        databin = lonarr(N_ELEMENTS(data))
        databin[i] = total(data[l[i]:m[i]])
    ENDFOR

END

wbin is the desired bin width. The junk variables only exist for the purpose of getting the min_subscripts at those locations. The min_subscripts are the l[i]’s and the m[i]’s.

I appreciate any help!!

Answer

The min_subscript argument is trying to pass a value back to you, so you must pass a “named variable” to it. Named variables have pass by reference behavior. So you have to do it in two steps, something like:

junk1 = min(abs(radius - radius[i]), li)
l[i] = li

Above, li is a named variable, so it can receive and pass back the value. Then you can put it in your storage array.

Leave a Reply

Your email address will not be published. Required fields are marked *