Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
From | wgould@stata.com (William Gould, StataCorp LP) |
To | statalist@hsphsun2.harvard.edu |
Subject | Re: st: Mata equivalent for R's which() or matlabs find() functions? |
Date | Tue, 01 Mar 2011 14:34:54 -0600 |
Sebastian Eppner <eppner@uni-potsdam.de> 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 wgould@stata.com * * 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/