Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
From | Joerg Luedicke <joerg.luedicke@gmail.com> |
To | statalist@hsphsun2.harvard.edu |
Subject | st: Re: Fwd: Linear indexing of matrices in Mata |
Date | Tue, 24 Jul 2012 09:27:10 -0500 |
Say we have 5 matrices M1,...,M5. Further assume that we want to use a subset of M2, M3, M4, M5 based on a selection in M1 (say, M1 :> 2). Using your function we would type: index_which(M2, (M1 :> 2)) index_which(M3, (M1 :> 2)) index_which(M4, (M1 :> 2)) index_which(M5, (M1 :> 2)) and so the expression is repeated four times, and evaluated four times. In R, we would type the expression only once and use index numbers thereafter: index=which(M1>2) M2[index] M3[index] M4[index] M5[index] Joerg On Mon, Jul 23, 2012 at 10:06 PM, Joseph Coveney <jcoveney@bigplanet.com> wrote: > > Joerg Luedicke wrote (excerpted): > > . . . Especially if you have several of the same expressions that > need to be evaluated, the code gets messy and it probably slows things > down as the expression has to be evaluated every time instead of just > once if it would be possible to index elements. > > -------------------------------------------------------------------------------- > > I don't understand what you mean here: I don't understand how > > index=which(m1>2) > m2[index] > > would be less messy or less slowed down than > > index_which(M2, (M1 :> 2)) > > when you have several of the same expressions that need to be evaluated. Are > you referring to vectorization or ?apply() or something? > > Could you illustrate what you mean with an example? > > Joseph Coveney > > * * 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/