Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: return the maximum and corresponding value...


From   Kit Baum <[email protected]>
To   [email protected]
Subject   st: return the maximum and corresponding value...
Date   Sun, 5 Nov 2006 14:13:09 -0500

Le Wang said

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.



The good news: there is a function undocumented in the manual to handle this.
The bad news: it does not appear to work.

This workaround does:

real scalar lewang(real matrix a)
{
real vector i
maxindex(a[.,1],1,i,w)
return(i)
}
a=(4,2\6,3\10,9\1,2\4,5\7,6\9,0)
j=lewang(a)
b=a[j,.]
b

Now all this horsing around should be unnecessary, for you should just be able to call

maxindex(a[.,1],1,ii,w)

But that yields

<istmt>: 3499 ii not found
r(3499);


minindex() doesn't seem to work either, even on the examples in the helpfile:


: v = (3,1,5,7,6)

: minindex(v, 1, i, w)
<istmt>: 3499 i not found
r(3499);


Looks like a bug to me.


Kit Baum, Boston College Economics
http://ideas.repec.org/e/pba1.html
An Introduction to Modern Econometrics Using Stata:
http://www.stata-press.com/books/imeus.html


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




© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index