Statalist


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

re: st: row number corresponding to a column maximum


From   Kit Baum <[email protected]>
To   [email protected]
Subject   re: st: row number corresponding to a column maximum
Date   Tue, 28 Jul 2009 18:04:24 -0400

<>
Gabi proposed

real scalar maxneighbor(real matrix A, real scalar i, real scalar j) {
real scalar k, r, max
r=rows(A)
max=colmax(A)[1,j]
k=1
while(A[k,j]<max) {
	k=k+1
}
return(A[k,i]) }

I can't see how that can work with colmax(A)[1,j] being hardcoded to look at only the first row of A.

A solution to the problem, I believe, is

capture mata mata drop maxneighbor()
mata
x = 1,55 \ 2,44 \ 3,33 \ 2,22 \ 2,11

real scalar maxneighbor(real matrix A, real scalar i, real scalar j)
{
    real scalar k, max
	maxindex(A[.,j], 1, ii, w)
	return(A[ii[1,1],i])
}

ival = maxneighbor( x, 2, 1)
ival
end

which will find the first instance of the maximum value in column j. It does not deal with ties.

Kit Baum   |   Boston College Economics & DIW Berlin   |   http://ideas.repec.org/e/pba1.html
An Introduction to Stata Programming | http://www.stata-press.com/books/isp.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/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