# st: RE: return the maximum and its corresponding value of a matrix in Mata

 From Jesper Kj�r Hansen <[email protected]> To <[email protected]> Subject st: RE: return the maximum and its corresponding value of a matrix in Mata Date Sun, 5 Nov 2006 20:14:25 +0100

```On Sunday, 2006/11/05, Le Wang [[email protected]] wrote:

> I have a question regarding Mata. Suppose I have an imaginary matrix
> with two columns as follows,
> x                      y
> 2.9479707       .15
> 2.926226         .16
> 2.8994321       .17
> 2.8691981       .18
> 2.8368784       .19
> 2.8035694        .2
> 2.7701303       .21
> 2.7372142       .22
> 2.705299         .23
> 2.6747184        .24
>
> I want Mata to return the maximum values of column 1 (x) AND its
> corresonding values in column 2 (y), in this example, 2.9479707 and
> 0.15. I understand I can use -rowmax- to find the maximum, but I am
> not quite sure how to find the corresponding value.
>

Have a look at the mata function maxindex()
(-help mata maxindex()-).

If the above data was collected in one matrix X,
you could then do something like:

i = J(0,1,.) ; w = J(0,2,.)
maxindex(X[,1], 1, i, w)

// max value might not be unique:
// one row per max value
output = J(length(i), 2, .)
output = ( X[i,1] , X[i,2] )

If values in X[,1] are unique, you don't have to

/Jesper

-- Jesper Kj�r Hansen
mailto:[email protected]

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
```

• References: