Mata course: Subscripting

1. Basic subscripts

syntax meaning

x[i, j] (i, j) element of x
x[i] ith element of x

Notes:

--
x[i] is allowed only if
x is r x 1 or 1 x c

--
may be used on left or right side of equal sign

--
z = x[i,j]

--
x[i,j] = z

--
very fast

2. Entire rows and columns

syntax meaning

x[i,.] ith row
x[.,j] jth column

x[i,] same as x[i,.]
x[,j] same as x[.,j]

Notes:

--
may be used on left or right side of equal sign

--
z = x[,j]

--
x[,j] = z

--
very fast

3. Specified rows and columns

In x[i,j],
i
and
j
may be vectors

-- good style:

-- i a column

-- j a row

but it does not matter

-- elements may be repeated

-- may be used on left or right of equal sign

-- reasonably fast

4. Submatrices (range subscripts)

--
x[|i1,j1 \ i2,j2|]
extracts the submatrix

--
with upper-left corner (i1,j1)

--
with lower-right corner (i2,j2)

--
x[|i1 \ j2|] extracts subvector

--
from i1 to i2

--
result is row if x is a row,
column if x is a column

--
very fast

Top