Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Matching elements of a list revisited

    Dear Mata users:
    I am writing because I previously got some wonderful suggestions about how to quickly find elements shared by two lists. The previous post on "common elements in a list" can be found under the mata topic "common elements in a list." Anyways: the latest problem I am hoping for insights into is one in which the elements of the list have multiple values. As an example, the following does what I want, but is pretty inefficient because of the use of a loop particularly when large dimensions are involved.

    Code:
    mata:
    A=1,1 \ 1,2 \ 2,1 \ 2,4 \ 4,5 \ 6,8
    B=1,1 \ 1,2 \ 1,3 \ 2,3 \ 3,3 \ 4,5 \ 6,9 \ 7,2
    
    indA=J(rows(A),1,0)
    
    for (i=1;i<=rows(A);i++) {
        if (anyof(rowsum(A[i,]:==B),2)) indA[i]=1
    }
    end
    The variable indA then contains ones in the position of the rows of A that appear in B, and zeros for all the other rows of A. To reiterate the question: is there a way to do this without looping and/or more quickly?

    Thanks in advance,

    Matt Baker




Working...
X