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: mata: minindex vs permutation vector for finding closest values


From   László Sándor <[email protected]>
To   [email protected]
Subject   st: mata: minindex vs permutation vector for finding closest values
Date   Fri, 11 May 2012 19:12:58 -0400

Hi all,
esp. Mata wizards among you. My code needs to find the closest L
elements of the same vector to many different numbers. Currently I use
-minindex- (a Mata function introduced in Stata 10) for vectors like
"p:-p[i]", which is speedy, but even that can be slow for datasets
with many thousands of observations (note that though -minindex-
operates on the aforementioned vector, it still needs to loop over all
i to generate and then process this vector. (The 'lookup' grows
roughly quadratic in the number of observations.)

But actually the ordering of the original vector could help. If I save
that order in a permutation vector, I could simply (?) find p[i] in
that vector and check the L items before and after.

Even though the first sort would be costlier than a minindex, my
impression is that it would be worth the effort. Do you agree? Is
there some useful function (or famous algorithm -- I'm no computer
scientist) that I should look up for this?

Thanks,

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


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