Statalist


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

Re: st: extract rowname corresponding to row index of matrix


From   Phil Schumm <pschumm@uchicago.edu>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: extract rowname corresponding to row index of matrix
Date   Sat, 11 Apr 2009 07:41:37 -0500

On Apr 1, 2009, at 5:57 PM, Jacob Wegelin wrote:
Suppose we have a matrix BETA with -rownames- and -colnames-.

Given the names of a row and of a column in BETA, it is possible to extract the indices of that row and of that column, as shown below.

But is there an equally simple approach to the reverse problem? I mean: Given a row index, is there an elegant way to extract the name of that row?


You can use the Mata functions st_matrixrowstripe() and st_matrixcolstripe(). For example, a minimal implementation of your getRowName program would be:


mata:
string scalar getRowName(string scalar matname, real scalar row)
{
    return(st_matrixrowstripe(matname)[row, 2])
}
end


Then, to get the name of the 5th row (i.e., "flea" in your example), you would use:


mata: getRowName("BETA", 5)


-- Phil

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