View Raw SPL
/*****************************************************************************
*                                                                            *
*   FINDCOL.SPL  Copyright (C) 2007 DSP Development Corporation              *
*                               All Rights Reserved                          *
*                                                                            *
*   Author:      Randy Race                                                  *
*                                                                            *
*   Synopsis:    Returns the columns that meet a condition                   *
*                                                                            *
*   Revisions:    5 Oct 2007  RRR  Creation                                  *
*                                                                            *
*****************************************************************************/


#if @HELP_FINDCOL

    FINDCOL

    Purpose: Returns the columns that meet a condition.

    Syntax:  FINDCOL(cond)

              cond - A binary series representing a condition.

    Returns: A series, the columns that meet the condition.

    Example:
             W1: ravel(1..12, 4);
             W2: findcol(W1 > 7);

             W1 == {{1, 5,  9},
                    {2, 6, 10},
                    {3, 7, 11},
                    {4, 8, 12}}

             W2 == {2, 3}

             Indicating that columns 2 and 3 meet the condtion W1 > 7.

    Remarks:
             A column value is returned only once even if multiple values in
             a column meet the condition.

    See Also:
             Find
             Findrow
#endif


/* find columns that meet a condition */
findcol(cond)
{
        local r, c, d;

        /* rows and cols that meet condition */
        (r, c) = find(cond);

        if (length(c) > 1)
        {

                /* sort columns in ascending order */
                c = sort(c, -1);

                /* find duplicates */
                d = extract(c - delay(c, 1), 2, -1);

                /* remove duplicates */
                c = delete(c, d == 0);
        }

        return(c);
}