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]

st: inlist() equivalent for mata?


From   Andrew Maurer <[email protected]>
To   Statalist Statalist <[email protected]>
Subject   st: inlist() equivalent for mata?
Date   Wed, 19 Mar 2014 16:24:47 +0000

Hi Statalist,

I am trying to find a mata equivalent of Stata's inlist() function, but have not found anything similar when looking through the directory of built-in mata functions. I have a few roundabout ideas, but they seem very complicated in order to a task that should be quite simple.

Goal: for a given vector, B, with elements b1,b2,...,bN, and list, L, of size T, return a vector of size N, where the ith element = 1 if bi is in L, otherwise 0. In my case T >> N and all elements are integers.

Example, let the list, L = {1, 3, 99}

Idea 1: Does mata have a conception of sparse matrices? The idea would be to create a vector, M, where only row 1, row 3, and row 99 exist. and the value for each element is 1. The inlist(b,L) function would return M[b] if M[b] exists, else return 0.

Idea 2: Create a separate pointer for every element in L, named "p" followed by the value of the element. Each pointer would point to the value 1. In this case, p1, p3, and p99 would be created, where *p1 = *p3 = *p99 = 1. The inlist(b,L) function would return *p`b' if *p`b' exists, else 0 (I'm not sure how to get around the stata local syntax here).

Idea 3: Use a hash table / associative array. It would make sense to create a perfect hash table, where the table is created such that every element of L maps to a unique value, but the only function I could find is hash1(), which can result in collisions. I wrote a working example using asarray()(see code below), but it seems somewhat inefficient.

Thank you,

Andrew Maurer


************ define inlist() *********************
cap mata mata drop inlist()
mata

real colvector inlist(real colvector B, real colvector L)
{

// initialize matrices
real matrix M
real colvector R

// create the map, M
M = asarray_create("real", 1, rows(L))
asarray_notfound(M, 0)

for (i=1; i<=rows(L); i++) {
	asarray(M, L[i], 1)
}

// create vector to be returned
R = J(rows(B),1,.)
for (i=1; i<=rows(B); i++) {
	R[i,1] = asarray(M, B[i])
}

return(R)

}

end
************ end inlist() definition *************


************ test inlist() *****************
mata
mylist = 1 \ 4 \ 7 \ 10
b1 = 3
b2 = 4
b3 = 1 \ 2 \ 3 \ 4

inlist(b1,mylist) // returns 0
inlist(b2,mylist) // returns 1
inlist(b3,mylist) // returns (1,0,0,1)'
end
************ end test inlist() *************


*
*   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