Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: Mata equivalent for R's which() or matlabs find() functions?


From   Nick Cox <[email protected]>
To   [email protected]
Subject   Re: st: Mata equivalent for R's which() or matlabs find() functions?
Date   Wed, 2 Mar 2011 00:49:19 +0000

Bill's approach is illustrated here only on examples in which each
column contains a single element. Consider an example from an earlier
post

: y = J(4,2,(1..4))

: y
       1   2   3   4   5   6   7   8
    +---------------------------------+
  1 |  1   2   3   4   1   2   3   4  |
  2 |  1   2   3   4   1   2   3   4  |
  3 |  1   2   3   4   1   2   3   4  |
  4 |  1   2   3   4   1   2   3   4  |
    +---------------------------------+

If we are testing whether any column is all 4s, then it seems we need
something like -colmax()- to produce conformable arguments.

: select((1..cols(y)), colmax(y :== J(4,1,4)))
       1   2
    +---------+
  1 |  4   8  |
    +---------+

In other cases in which a condition could be true or false for
individual elements in a column

: x = J(2,4,(1,2,3,4\4,3,2,1))

: x
        1    2    3    4    5    6    7    8    9   10   11   12   13
 14   15   16
    +---------------------------------------------------------------------------------+
  1 |   1    2    3    4    1    2    3    4    1    2    3    4    1
  2    3    4  |
  2 |   4    3    2    1    4    3    2    1    4    3    2    1    4
  3    2    1  |
  3 |   1    2    3    4    1    2    3    4    1    2    3    4    1
  2    3    4  |
  4 |   4    3    2    1    4    3    2    1    4    3    2    1    4
  3    2    1  |
    +---------------------------------------------------------------------------------+

we need -colmin()- or -colmax()- to distinguish between "any true" and
"all true".

: select((1..cols(x)), colmax(x :>= 3))
        1    2    3    4    5    6    7    8    9   10   11   12   13
 14   15   16
    +---------------------------------------------------------------------------------+
  1 |   1    2    3    4    5    6    7    8    9   10   11   12   13
 14   15   16  |
    +---------------------------------------------------------------------------------+

: select((1..cols(x)), colmin(x :>= 3))

: select((1..cols(x)), colmin(x :>= 2))
        1    2    3    4    5    6    7    8
    +-----------------------------------------+
  1 |   2    3    6    7   10   11   14   15  |
    +-----------------------------------------+

Incidentally, I like to test for even and odd using -mod(,)-.

: mod(2,2) == 0
  1

: mod(3,2) == 0
  0

: !mod(3,2)
  0

: !mod(2,2)
  1

Nick


On Tue, Mar 1, 2011 at 8:34 PM, William Gould, StataCorp LP
<[email protected]> wrote:
> Sebastian Eppner <[email protected]> writes,
>
>> I am desperately looking for a mata function that gives me the column
>> indices of columns that satisfy certain conditions.
>
> Just to fix ideas, let's say we have
>
>      x = (2, 4, 1, 6)
>
> and we want to obtain the column indices for which x is even.  We
> want (1, 2, 4).  The answer is:
>
>        : s = (floor(x:/2)*2 :== x)
>
>        : result = select((1..4), s)
>
>        : result
>               1   2   3
>            +-------------+
>          1 |  1   2   4  |
>            +-------------+
>
> In the above, s is the selection vector; the vector contains 0 for
> columns that should be omitted, and nonzero for columns to be included.
> I set up s to contain 1s and 0s, with 1s marking the even numbers.
>
> (1..4) creates the vector (1, 2, 3, 4).
>
> (1..6) would create (1, 2, 3, 4, 5, 6)
>
> Thus, (1..cols(s)), which Mata will understand, creates a vector of
> indices.
>
> The select() function selects columns (or rows) from one matrix based on
> a selection vector.
>
> -- Bill
> [email protected]

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index