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: inlist() equivalent for mata?


From   Phil Schumm <[email protected]>
To   Statalist Statalist <[email protected]>
Subject   Re: st: inlist() equivalent for mata?
Date   Thu, 20 Mar 2014 20:09:46 -0500

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/


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