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]
st: RE: Mata equivalent for R's which() or matlabs find() functions?
From
Nick Cox <[email protected]>
To
"'[email protected]'" <[email protected]>
Subject
st: RE: Mata equivalent for R's which() or matlabs find() functions?
Date
Tue, 1 Mar 2011 17:35:21 +0000
I think the test is whether what is needed can be written if there is no obvious candidate. Perhaps there is; but here is a sketch:
real colfind(transmorphic matrix M, transmorphic colvector v) {
real j
rowvector indices
indices = J(1,0,.)
for(j = 1; j <= cols(M); j++) {
if (M[,j] == v) indices = indices , j
}
return(indices)
}
: 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 |
+---------------------------------+
: colfind(y, J(4,1,4))
1 2
+---------+
1 | 4 8 |
+---------+
: text = J(1,4,("foo", "bar"))
: text
1 2 3 4 5 6 7 8
+-------------------------------------------------+
1 | foo bar foo bar foo bar foo bar |
+-------------------------------------------------+
: colfind(text, "foo")
1 2 3 4
+-----------------+
1 | 1 3 5 7 |
+-----------------+
Nick
[email protected]
Sebastian Eppner
I am desperately looking for a Mata function that gives me the column
indices of columns that satisfy certain conditions.
For MATLAB, find() can solve the job. In R, which() does it.
I look for something like this in Mata, but haven't found anything
yet. It seems too basic to not be included...
*
* 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/