View Raw SPL
/*****************************************************************************
*                                                                            *
*   ROWAVG.SPL   Copyright (C) 2021 DSP Development Corporation              *
*                               All Rights Reserved                          *
*                                                                            *
*   Author:      Randy Race                                                  *
*                                                                            *
*   Synopsis:    Produces a column of the means of each row of an array      *
*                                                                            *
*   Revisions:   29 Oct 2021  RRR  Creation                                  *
*                                                                            *
*****************************************************************************/


#if @HELP_ROWAVG

    ROWAVG

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

    Syntax:  ROWAVG(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 = rowavg(a)

             b == {4, 10}

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

             d = rowavg(a)

             d == {4, nan}

    Remarks:
             ROWAVG does not assume the number of columns for each row is
             the same.

             Unlike ROWMEAN, ROWAVG does not ignore NaN values.

             For data known not to contain NaN values, ROWAVG is faster
             than ROWMEAN and produces the same result.

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


/* calculate the average of each row */
rowavg(a)
{
        local ncol, is_rect, rmean;

        (ncol, is_rect) = numcols(a);

        if (not(is_rect))
        {
                ncol = rowlen(a);
        }

        rmean = rowsum(a) / ncol;

        return(rmean);
}