Statalist


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

RE: st: row number corresponding to a column maximum


From   Maarten buis <[email protected]>
To   stata list <[email protected]>
Subject   RE: st: row number corresponding to a column maximum
Date   Tue, 28 Jul 2009 20:59:04 +0000 (GMT)

--- Gabi Huiber wrote:
> How do you make Stata return the row number that corresponds to the
> maximum value in a given column, in Mata or Stata?

What about this example?

*---------------- begin example -------------
mata
x = 1 \ 2 \ 3 \ 2 \ 3

val = x[1,1]
rownum = 1
for(i = 2; i <= rows(x); i++){
	if(x[i,1]==val){
		rownum = rownum, i
	}
	if(x[i,1]>val) {
		val = x[i,1]
		rownum = i
	}
}
val 
rownum
end
*------------------ end example ------------------

Hope this helps,
Maarten

-----------------------------------------
Maarten L. Buis
Institut fuer Soziologie
Universitaet Tuebingen
Wilhelmstrasse 36
72074 Tuebingen
Germany

http://home.fsw.vu.nl/m.buis/
-----------------------------------------


      

*
*   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–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index