Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, statalist.org is already up and running.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

st: Re: Fwd: Linear indexing of matrices in Mata


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/


© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index