View Raw SPL
/*****************************************************************************
*                                                                            *
*   ROWMEAN.SPL      Copyright (C) 2004, 2014 DSP Development Corporation    *
*                               All Rights Reserved                          *
*                                                                            *
*   Author:      Randy Race                                                  *
*                                                                            *
*   Synopsis:    Produces a column of the means of each row of an array      *
*                                                                            *
*   Revisions:   30 Mar 2004  RRR  Creation                                  *
*                18 Aug 2014  RRR  removena                                  *
*                                                                            *
*****************************************************************************/


#if @HELP_ROWMEAN

    ROWMEAN

    Purpose: Produces a column of the means of each row of a table.

    Syntax:  ROWMEAN(table)

              table - A series, table or expression evaluating to a series
                      or table.

    Returns: A single column series with the same number of rows as the
             input table.

    Example:
             a = {{2,  4,  6},
                  {8, 10, 12}}

             b = rowmean(a)

             b == {4, 10}


    Example:
             a = {{2,  4,   6},
                  {8, 10, nan}}

             b = rowmean(a)

             b == {4, 9}    
Remarks:
             ROWMEAN does not assume the number of columns for each row is
             the same.

             ROWMEAN ignores NaN values. See ROWAVG for a faster implementation
             if the data is known not to contain NaN values.

    See Also:
             Colmean
             Mean
             Row
             Rowavg
             Rowlen
             Rowmax
             Rowmin
             Rowreduce
             Rowstdev
             Rowsum
             Transpose
#endif


/* calculate the mean of each row */
rowmean(a)
{
        local rmean, vu, no_na;

        if (anynan(a))
        {
                /* handle nan */
                no_na = setnavlue(a, 0);

                rmean = rowsum(no_na);

                /* get units */
                vu = getvunits(rmean);

                /* divide by length of each column */
                rmean /= collength(no_na);

                /* set units */
                setvunits(rmean, vu);
        }
        else
        {
                rmean = rowavg(a);
        }

        return(rmean);
}