📄 m2_subscripts.hlp
字号:
{cmd:x[|1,4 \ 1,4|] = I(4)}
{title:When to use list subscripts and when to use range subscripts}
{p 4 4 2}
Everything a range subscript can do, a list subscript can also do. The
one seemingly unique feature of a range subscript
{it:x}{cmd:[|}{it:i1}{cmd:,}{it:j1} {cmd:\} {it:i2}{cmd:,}{it:j2}{cmd:|]}
{p 4 4 2}
is perfectly mimicked by
{it:x}{cmd:[(}{it:i1}{cmd:::}{it:i2}{cmd:), (}{it:j1}{cmd:..}{it:j2}{cmd:)]}
{p 4 4 2}
The range-subscript construction, however, executes more quickly, and so that
is the purpose of range subscripts: to provide a fast way to extract
contiguous submatrices. In all other cases, use list subscripts because they
are faster.
{p 4 4 2}
Use list subscripts to reference scalar values:
{cmd:result = x[1,3]}
{cmd:x[1,3] = 2}
{p 4 4 2}
Use list subscripts to extract entire rows or columns:
{cmd:obs = x[., 3]}
{cmd:var = x[4, .]}
{p 4 4 2}
Use list subscripts to permute the rows and columns of matrices:
{cmd:x = (1,2,3,4 \ 5,6,7,8 \ 9,10,11,12)}
{cmd:y = x[(1\3\2), .]}
{cmd:y}
{res} {txt} 1 2 3 4
{c TLC}{hline 21}{c TRC}
1 {c |} {res} 1 2 3 4{txt} {c |}
2 {c |} {res} 9 10 11 12{txt} {c |}
3 {c |} {res} 5 6 7 8{txt} {c |}
{c BLC}{hline 21}{c BRC}
{cmd:y = x[., (1,3,2,4)]}
{cmd:y}
{res} {txt} 1 2 3 4
{c TLC}{hline 21}{c TRC}
1 {c |} {res} 1 3 2 4{txt} {c |}
2 {c |} {res} 5 7 6 8{txt} {c |}
3 {c |} {res} 9 11 10 12{txt} {c |}
{c BLC}{hline 21}{c BRC}
{cmd:y = x[(1\3\2), (1,3,2,4)]}
{cmd:y}
{res} {txt} 1 2 3 4
{c TLC}{hline 21}{c TRC}
1 {c |} {res} 1 3 2 4{txt} {c |}
2 {c |} {res} 9 11 10 12{txt} {c |}
3 {c |} {res} 5 7 6 8{txt} {c |}
{c BLC}{hline 21}{c BRC}
{p 4 4 2}
Use list subscripts to duplicate rows or columns:
{cmd:x = (1,2,3,4 \ 5,6,7,8 \ 9,10,11,12)}
{cmd:y = x[(1\2\3\1), .]}
{cmd:y}
{res} {txt} 1 2 3 4
{c TLC}{hline 21}{c TRC}
1 {c |} {res} 1 2 3 4{txt} {c |}
2 {c |} {res} 5 6 7 8{txt} {c |}
3 {c |} {res} 9 10 11 12{txt} {c |}
4 {c |} {res} 1 2 3 4{txt} {c |}
{c BLC}{hline 21}{c BRC}
{cmd:y = x[., (1,2,3,4,2)]}
{cmd:y}
{res} {txt} 1 2 3 4 5
{c TLC}{hline 26}{c TRC}
1 {c |} {res} 1 2 3 4 2{txt} {c |}
2 {c |} {res} 5 6 7 8 6{txt} {c |}
3 {c |} {res} 9 10 11 12 10{txt} {c |}
{c BLC}{hline 26}{c BRC}
{cmd:y = x[(1\2\3\1), (1,2,3,4,2)]}
{cmd:y}
{res} {txt} 1 2 3 4 5
{c TLC}{hline 26}{c TRC}
1 {c |} {res} 1 2 3 4 2{txt} {c |}
2 {c |} {res} 5 6 7 8 6{txt} {c |}
3 {c |} {res} 9 10 11 12 10{txt} {c |}
4 {c |} {res} 1 2 3 4 2{txt} {c |}
{c BLC}{hline 26}{c BRC}
{title:A fine distinction}
{p 4 4 2}
There is a fine distinction between
{it:x}{cmd:[}{it:i}{cmd:,}{it:j}{cmd:]}
and
{it:x}{cmd:[|}{it:i}{cmd:,}{it:j}{cmd:|]}.
In {it:x}{cmd:[}{it:i}{cmd:,}{it:j}{cmd:]}, there are two arguments,
{it:i} and {it:j}. The comma separates the arguments.
In {it:x}{cmd:[|}{it:i}{cmd:,}{it:j}{cmd:|]},
there is one argument: {it:i}{cmd:,}{it:j}. The comma is the
column-join operator.
{p 4 4 2}
Mostly comma in Mata means the column-join operator:
{cmd:newvec = oldvec, addedvalues}
{cmd:qsum = (x,1)'(x,1)}
{p 4 4 2}
There are, in fact, only two exceptions. When you type the arguments
for a function, the comma separates one argument from the next:
{cmd:result = f(}{it:a}{cmd:,}{it:b}{cmd:,}{it:c}{cmd:)}
{p 4 4 2}
In the above example, {cmd:f()} receives three arguments: {it:a}, {it:b}, and
{it:c}. If we wanted {cmd:f()} to receive one argument,
({it:a},{it:b},{it:c}), we would have to enclose the calculation in
parentheses:
{cmd:result = f((}{it:a}{cmd:,}{it:b}{cmd:,}{it:c}{cmd:))}
{p 4 4 2}
That is the first exception. When you type the arguments inside a
function, comma means argument separation. You get back to the usual
meaning of comma -- the column-join operator -- by opening another set of
parentheses.
{p 4 4 2}
The second exception is in list subscripting:
{it:x}{cmd:[}{it:i}{cmd:,}{it:j}{cmd:]}
{p 4 4 2}
Inside the list-subscript brackets, comma means argument separation.
That is why you have seen us type vectors inside parentheses:
{it:x}{cmd:[(1\2\3),(1,2,3)]}
{p 4 4 2}
These are the two exceptions. Range subscripting is not an exception. Thus
in
{it:x}{cmd:[|}{it:i}{cmd:,}{it:j}{cmd:|]}
{p 4 4 2}
there is one argument, {it:i}{cmd:,}{it:j}. With range subscripts,
you may program constructs such as
{cmd:IJ = (}{it:i}{cmd:,}{it:j}{cmd:)}
{cmd:RANGE = (1,2 \ 4,4)}
...
... {it:x}{cmd:[|IJ|]} ... {it:x}{cmd:[|RANGE|]} ...
{p 4 4 2}
You may not code in this way using list subscripts. In particular,
{it:x}{cmd:[IJ]} would be interpreted as a request to extract elements {it:i}
and {it:j} from vector {it:x}, and would be an error otherwise.
{it:x}{cmd:[RANGE]} would always be an error.
{p 4 4 2}
We said earlier that list subscripts
{it:x}{cmd:[}{it:i}{cmd:,}{it:j}{cmd:]}
are a little faster than range subscripts
{it:x}{cmd:[|}{it:i}{cmd:,}{it:j}{cmd:|]}.
That is true, but if {cmd:IJ}={cmd:(}{it:i}{cmd:,}{it:j}{cmd:)} already,
{it:x}{cmd:[|IJ|]} is faster than
{it:x}{cmd:[}{it:i}{cmd:,}{it:j}{cmd:]}.
You would, however, have to execute
many millions of references to
{it:x}{cmd:[|IJ|]}
before you could measure the
difference.
{title:Conformability}
{it:x}{cmd:[}{it:i}{cmd:,} {it:j}{cmd:]}:
{it:x}: {it:r x c}
{it:i}: {it:m} {it:x} 1 or 1 {it:x} {it:m} (does not matter which)
{it:j}: 1 {it:x} {it:n} or {it:n} {it:x} 1 (does not matter which)
{it:result}: {it:m x n}
{it:x}{cmd:[}{it:i}{cmd:, .]}:
{it:x}: {it:r x c}
{it:i}: {it:m} {it:x} 1 or 1 {it:x} {it:m} (does not matter which)
{it:result}: {it:m x c}
{it:x}{cmd:[.,} {it:j}{cmd:]}:
{it:x}: {it:r x c}
{it:j}: 1 {it:x} {it:n} or {it:n} {it:x} 1 (does not matter which)
{it:result}: {it:r x n}
{it:x}{cmd:[., .]}:
{it:x}: {it:r x c}
{it:result}: {it:r x c}
{it:x}{cmd:[}{it:i}{cmd:]}:
{it:x}: {it:n x} 1 1 {it:x n}
{it:i}: {it:m x} 1 or 1 {it:x m} 1 {it:x m} or {it:m x} 1
{it:result}: {it:m x} 1 1 {it:x m}
{it:x}{cmd:[.]}:
{it:x}: {it:n x} 1 1 {it:x n}
{it:result}: {it:n x} 1 1 {it:x n}
{it:x}{cmd:[|}{it:k}{cmd:|]}:
{it:x}: {it:r x c}
{it:k}: 1 {it:x} 2
{it:result}: 1 {it:x} 1 if {it:k}[1]<. & {it:k}[2]<.
{it:r x} 1 if {it:k}[1]>=. & {it:k}[2]<.
1 {it:x c} if {it:k}[1]<. & {it:k}[2]>=.
{it:r x c} if {it:k}[1]>=. & {it:k}[2]>=.
{it:x}{cmd:[|}{it:k}{cmd:|]}:
{it:x}: {it:r x c}
{it:k}: 2 {it:x} 2
{it:result}: {it:k}[2,1]-{it:k}[1,1]+1 {it:x} {it:k}[2,2]-{it:k}[1,2]+1
(in the above formula, if {it:k}[2,1]>=., treat as if {it:k}[2,1]={it:r},
and similarly, if {it:k}[2,2]>=., treat as if {it:k}[,2,]={it:c})
{it:x}{cmd:[|}{it:k}{cmd:|]}:
{it:x}: {it:r x} 1 1 {it:x c}
{it:k}: 2 {it:x} 1 2 x 1
{it:result}: {it:k}[2]-{it:k}[1]+1 {it:x} 1 1 {it:x} {it:k}[2]-{it:k}[1]+1
(if {it:k}[2]>=., treat as (if {it:k}[2]>=., treat as
if {it:k}[2]={it:r}) if {it:k}[2]={it:c})
{title:Diagnostics}
{p 4 4 2}
Both styles of subscripts abort with error if the subscript is out of
range, if a reference is made to a nonexisting row or column.
{title:Also see}
{p 4 13 2}
Manual: {hi:[M-2] subscripts}
{p 4 13 2}
Online: help for
{bf:{help m2_intro:[M-2] intro}}
{p_end}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -