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: inlist() equivalent for mata?
From
Aljar Meesters <[email protected]>
To
[email protected]
Subject
Re: st: inlist() equivalent for mata?
Date
Fri, 21 Mar 2014 14:15:52 +0100
For this implementation I assume that you only have positive integers.
It is pretty fast and scales nicely in N and T. A caveat is that it
consumes memory based on the largest integers in your list and vector.
Best,
Aljar
*** Begin ***
real colvector vec_inlist(real colvector B, real colvector L){
real colvector b, l
real scalar minrows
// Determine the elements in B
b = J(max(B), 1, 0)
b[B] = J(rows(B), 1, 1)
// Determine the elements in L
l = J(max(L), 1, 0)
l[L] = J(rows(L), 1, 1)
// Combine the combination
minrows = min((rows(b), rows(l)))
answer = J(rows(b), 1, 0)
answer[|1, 1 \ minrows, 1 |] = b[|1, 1 \ minrows, 1 |] :* l[|1, 1
\ minrows, 1 |]
// Reflect back to B
answer= answer[B]
return(answer)
}
*** End ***
2014-03-21 2:09 GMT+01:00 Phil Schumm <[email protected]>:
> On Mar 20, 2014, at 4:42 PM, Andrew Maurer <[email protected]> wrote:
>> So with Bsize of 1 billion and Lsize of 100,000, it would effectively do a loop of 1 billion iterations over 100,000 elements.
>
>
> In your original post, you said that "T >> N", where T is the length of list L, and N is the length of list B. This would appear to be the opposite of what you wrote above.
>
>
>> I still think that there should be a more efficient way to do this than the array method posted.
>
>
> The fastest (and simplest) option is to use -anyof()-, e.g.,
>
>
> real colvector inlist_anyof(real colvector B, real colvector L)
> {
> real scalar i
> real colvector R
>
> R = J(rows(B),1,0)
> for (i=1; i<=rows(B); i++) {
> if (anyof(L, B[i])) R[i] = 1
> }
>
> return(R)
> }
>
>
> -- Phil
>
>
> *
> * For searches and help try:
> * http://www.stata.com/help.cgi?search
> * http://www.stata.com/support/faqs/resources/statalist-faq/
> * http://www.ats.ucla.edu/stat/stata/
*
* For searches and help try:
* http://www.stata.com/help.cgi?search
* http://www.stata.com/support/faqs/resources/statalist-faq/
* http://www.ats.ucla.edu/stat/stata/