Statalist


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

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


From   Jacob Wegelin <[email protected]>
To   [email protected]
Subject   Re: st: subsetting a matrix by rowname and column name
Date   Tue, 24 Mar 2009 14:24:54 -0400

Thanks to Austin Nichols for this insight.

But a slight modification of the code seems to reveal the following:
Although a variable and a scalar can both have the same name (for
example, -b- in Austin Nichols's exammple), it appears that a matrix
and a scalar
 cannot have the same name.  Is this correct?  See what happens below
when I try to -matrix list- b after scalar b has been defined.

   sysuse auto, clear
   g b=1
   qui probit foreign mpg
   mat b=e(b)
   mat lis b
   scalar b=el(matrix(b),1,colnumb(matrix(b),"mpg"))
   di scalar(b)
   mat lis b   /* generates an error */
   di b

Am I missing something?

Jake



On Sat, Mar 21, 2009 at 6:09 PM, Austin Nichols <[email protected]> wrote:
> 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/
>

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