Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.

# st: Fwd: Linear indexing of matrices in Mata

 From Joerg Luedicke To statalist@hsphsun2.harvard.edu Subject st: Fwd: Linear indexing of matrices in Mata Date Mon, 23 Jul 2012 10:36:04 -0500

```Just realized that I accidentally sent this to Joseph privately

---------- Forwarded message ----------
From: Joerg Luedicke <joerg.luedicke@gmail.com>
Date: Mon, Jul 23, 2012 at 9:57 AM
Subject: Re: Linear indexing of matrices in Mata
To: Joseph Coveney <jcoveney@bigplanet.com>

Thanks, Joseph. I would also think that there must be a more elegant
solution. 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.

Anyway, this is a good workaround.

Thanks again,

Joerg

On Sat, Jul 21, 2012 at 10:13 PM, Joseph Coveney <jcoveney@bigplanet.com> wrote:
>
> Joerg Luedicke wrote (excerpted):
>
> . . . So my first question
> would be: is my impression correct that there is no linear indexing
> for matrices in Mata?
>
> If yes, the second question would be how to best circumvent this?
> Consider the following problem: suppose we have two matrices, m1 and
> m2 with the same dimensions. Now we want to check a certain condition
> in m1 and if true, record the position index. Then we want to select
> elements in m2 that are corresponding to the recorded positions. For
> example, if we type in R
>
> *--R code--
> m1=matrix(c(1,2,3,4), nrow = 2)
> m2=matrix(c(5,6,7,8), nrow = 2)
> index=which(m1>2)
> m2[index]
> *-------------
>
> then m1 looks like
>
>      [,1] [,2]
> [1,]    1    3
> [2,]    2    4
>
> and m2 looks like
>
>      [,1] [,2]
> [1,]    5    7
> [2,]    6    8
>
> Then, with the which() function we create a vector of integers that
> denote the position in the matrix m1 - for which the element is
> greater than 2 - by a single number. In this example, these are matrix
> positions 3 and 4 (coinciding with the values in that matrix).
> Finally, we can use the indices to index elements, as -m2[index]-
> yields
>
> [1] 7 8
>
> i.e., the elements of m2 with the linear indices 3 and 4.
>
> So, the basic problem is how to refer to elements of a matrix, based
> on a selection of elements in another matrix. The above example
> demonstrates how this could be done in R. Any ideas about solutions in
> Mata?
>
> --------------------------------------------------------------------------------
>
>
> I'm not aware of linear indexing for matrixes in Mata.  Others more
> knowledgeable than I could ever hope to be might be able to point to such
> a feature, though.
>
> You would circumvent this by using Mata's -select()- function.
>
> One solution in Mata would be:
>
>     select(vec(m2)', vec(m1 :> 2)')
>
> There are undoubtedly other, more elegant, solutions.  This one is illustrated
> below using your two matrices.  It assumes that the corresponding R code returns
> only row vectors.  I've incorporated the statement into a rudimentary function
> for convenience, but you'd want to add some conformability- and type-checking
> for production use.
>
> Joseph Coveney
>
> . version 11.2
>
> .
> . clear *
>
> . set more off
>
> .
> . mata:
> ------------------------------------------------- mata (type end to exit) ------
> : mata set matastrict on
>
> :
> : real rowvector index_which(real matrix Target, real matrix Selector)
>>     return(select(vec(Target)', vec(Selector)'))
>
> :
> : M1 = (1, 3 \ 2, 4)
>
> : M2 = (5, 7 \ 6, 8)
>
> :
> : index_which(M2, (M1 :> 2))
>        1   2
>     +---------+
>   1 |  7   8  |
>     +---------+
>
> :
> : end
> --------------------------------------------------------------------------------
> .
> . exit
>
> end of do-file
>
>
>
*
*   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/
```