Statalist The Stata Listserver


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

Re: st: RE: return the maximum and corresponding value...


From   "Le Wang" <[email protected]>
To   [email protected]
Subject   Re: st: RE: return the maximum and corresponding value...
Date   Sun, 5 Nov 2006 14:07:07 -0600

Thank you all. Both -maxindex- and Nick's -sortx- worked well!

Le

On 11/5/06, German Rodriguez <[email protected]> wrote:
Kit,

I think it works if you define ii (and w) before calling maxindex. Try:
---
m = (
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)

ii = J(0,0,.)
w  = J(0,0,.)

maxindex(m[,1],1,ii,w)
---
Then

:ii
 1

: w
      1   2
   +---------+
 1 |  1   1  |
   +---------+

And the answer to Le Wang's question would be

m[ii,2]
 .15

This example happens to have the max on the first row. Let's move it to the
third

---
j = (2\3\1\4::10)
m2 = m[j,]
maxindex(m2[,1],1,ii,w)
---

: ii
 3

: m2[ii,2]
 .15

Curiously w is still (1,1). What is it?

Cheers,
Germ�n
-----Original Message-----
From: [email protected]
[mailto:[email protected]] On Behalf Of Kit Baum
Sent: Sunday, November 05, 2006 2:13 PM
To: [email protected]
Subject: st: return the maximum and corresponding value...

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/


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


--
~~~~~~~~~~~~~~~~~~~~~~~~~~~
Le Wang, Ph.D.
Minnesota Population Center
University of Minnesota
(o) 612-624-5818

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