问题
So defining a matrix named "Z", and displaying its first element works nicely:
. matrix Z = J(1,3,0)
. matrix list Z
Z[1,3]
c1 c2 c3
r1 0 0 0
. di el(Z,1,1)
0
On the other hand simply changing the name to "P" breaks the function of el()
:
. matrix P = J(1,3,0)
. matrix list P
P[1,3]
c1 c2 c3
r1 0 0 0
. di el(P,1,1)
type mismatch
Why?
Update 1:
While finding the above behavior (during a debug session), I reproduced from command line and from an ado file, then reproduced after clearing Stata with clear
, drop program _all
and matrix drop _all
. However, upon restarting Stata, I am unable to reproduce the behavior.
Update 2:
At least I thought I used clear
... further investigation shows that I can reproduce the behavior on restart if I have a variable with a name starting with "P" in memory. For example (starting Stata fresh):
. matrix P = J(1,3,0)
. matrix list P
. di el(P,1,1)
0
. set obs 100
obs was 0, now 100
. gen Parsnips = uniform()
(100 real changes made)
. di el(P,1,1)
type mismatch
. rename Parsnips parsnips
. di el(P,1,1)
0
回答1:
This works fine for me:
matrix Z = J(1,3,0)
matrix list Z
di el(Z,1,1)
matrix P = J(1,3,0)
matrix list P
di el(P,1,1)
Are you omitting information?
回答2:
Indeed Stata does collide with naming ambiguity in such a case as my second update indicates. Per feedback I just received from their technical support, using di el(matrix(P),1,1)
to explicitly refer to P
as a matrix resolves this issue.
来源:https://stackoverflow.com/questions/25693145/does-stata-choke-on-matrices-named-p