Statalist


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

Re: st: subsetting a matrix by rowname and column name


From   Austin Nichols <[email protected]>
To   [email protected]
Subject   Re: st: subsetting a matrix by rowname and column name
Date   Sat, 21 Mar 2009 18:09:50 -0400

Jacob Wegelin <[email protected]> :
Try:

sysuse auto, clear
g b=1
qui probit foreign mpg
mat b=e(b)
scalar b=el(matrix(b),1,colnumb(matrix(b),"mpg"))
di scalar(b)
di b

Note that matrices, scalars, and variables share the same namespace,
which often leads to cryptic error messages or huge mistakes for the
less careful (you may prefer to use a local instead to avoid this
problem).  So say matrix(b) instead of b, and scalar(p) instead of p.

On Sat, Mar 21, 2009 at 5:43 PM, Jacob Wegelin <[email protected]> wrote:
> The results below indicate that
>
> (1) One can extract a cell from a matrix using the row and column names, but
>
> (2) the value returned cannot be assigned to a scalar, only to a
> one-by-one matrix. (See error below.)
>
> (3) By contrast, when one extracts a cell from a matrix using row and
> column *indices*, the value can be assigned to a scalar.
>
> Is there some good reason for this? I'd ideally like to extract a
> scalar by row and column name, since this would be less error-prone.
>
> Thanks for any insights
*
*   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