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:108
    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.harrisgeospatial.com/doc...anges.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.


    MOST RECENT

    Effectively Using Imagery for Precision Agriculture

    6/22/2017

    Tight profit margins are nothing new to growers. With a large portion of budgets tied up in fixed costs,... more »

    Running Harris ENVI in the Cloud on Frame

    6/19/2017

    Frame Blog – June 19, 2017 – See how companies like Frame are using ENVI in the Cloud at... more »

    SIGN UP & STAY INFORMED

    Sign up to receive the latest news, events, technologies and special offers.

    SIGN ME UP