X
PrevPrev Go to previous topic
NextNext Go to next topic
Last Post 30 May 2017 11:33 AM by  David Starbuck
Issues with the MEAN function
 1 Replies
Sort:
You are not authorized to post a reply.
Author Messages

SH



New Member


Posts:7
New Member


--
28 May 2017 09:54 AM
    I am stymied by something that IDL is doing in my code, and I cannot explain it. I am calculating the mean (average) of a defined subset of an array (using an index defined by the where function), and I am printing out the mean as well as the number of elements in the subset for which a certain condition is met. If no element meets the condition (index = -1) , I would expect the mean to come out as NaN. However, IDL always returns a number. I thought something was wrong in my elaborate code, but then I did the very simple example below, and I cannot explain what is going on at the seventh prompt! It seems to be a bug, as the same thing using standard deviation (STDDEV function) instead of the mean does return NaN. Any workaround?

    IDL> data=[1,2,3,4,5,6,7,8,9]
    IDL> m=mean(data)
    IDL> print, m
    5.00000
    IDL> index=where(data gt 10)
    IDL> print, index
    -1
    IDL> m=mean(data[index])
    IDL> print, m
    9.00000
    IDL> m=mean(data)
    IDL> print, m
    5.00000
    IDL> index=where (data le 2)
    IDL> m=mean(data[index])
    IDL> print, m
    1.50000


    David Starbuck



    Basic Member


    Posts:143
    Basic Member


    --
    30 May 2017 11:33 AM
    If the index is "-1", IDL will return the last element in an array:

    IDL> a = [2,3,4,5]
    IDL> a[-1]
    5

    This is shorthand notation for SIZE-1, for example:

    IDL> a[N_ELEMENTS(a)-1]
    5

    This is discussed on the following help page:
    http://www.harrisgeospati...ubscript_Ranges.html

    My recommendation is to use the NULL keyword when calling the WHERE function. For example:

    IDL> data=[1,2,3,4,5,6,7,8,9]
    IDL> m=mean(data)
    IDL> index=where(data gt 10, /NULL)
    IDL> m=mean(data[index])
    % TOTAL: Variable is undefined: X.
    % Execution halted at: $MAIN$
    IDL> data[index]
    !NULL

    -David
    Harris GS
    You are not authorized to post a reply.