help mata select()
-------------------------------------------------------------------------------
Title
[M-5] select() -- Select rows or columns
Syntax
transmorphic matrix select(transmorphic matrix X, real vector v)
void st_select(A, transmorphic matrix X, real vector v)
Description
select(X, v) returns X
1. omitting the rows for which v[i]==0 (v a column vector) or
2. omitting the columns for which v[j]==0 (v a row vector).
st_select(A, X, v) does the same thing, except that the result is placed
in A and, if X is a view, A will be a view.
Remarks
Remarks are presented under the following headings:
Examples
Using st_select()
Examples
1. To select rows 1, 2, and 4 of 5 x c matrix X,
submat = select(X, (1\1\0\1\0))
See [M-2] subscripts for another solution, submat = X[(1\2\4), .].
2. To select columns 1, 2, and 4 of r x 5 matrix X,
submat = select(X, (1,1,0,1,0))
See [M-2] subscripts for another solution, submat = X[., (1,2,4)].
3. To select rows of X for which the first element is positive,
submat = select(X, X[.,1]:>0)
4. To select columns of X for which the first element is positive,
submat = select(X, X[1,.]:>0)
5. To select rows of X for which there are no missing values,
submat = select(X, rowmissing(X):==0)
6. To select rows and columns of square matrix X for which the diagonal
elements are positive,
pos = diagonal(X):>0
submat = select(X, pos)
submat = select(submat, pos')
or, equivalently,
pos = diagonal(X):>0
submat = select(select(X, pos), pos')
Using st_select()
Coding
st_select(submat, X, v) (1)
produces the same result as coding
submat = st_select(X, v) (2)
The difference is in how the result is stored. If X is a view (it need
not be), then (1) will produce submat as a view or, if you will, a
subview, whereas in (2), submat will always be a regular (nonview)
matrix.
When X is a view, (1) executes more quickly than (2) and produces a
result that consumes less memory.
See [M-5] st_view() for a description of views.
Conformability
select(X, v)
X: r1 x c1
v: r1 x 1 or 1 x c1
result: r2 x c1 or r1 x c2, r2 <= r1, c2 <= c1
st_select(A, X, v)
input:
X: r1 x c1
v: r1 x 1 or 1 x c1
output:
A: r2 x c1 or r1 x c2, r2 <= r1, c2 <= c1
Diagnostics
None.
Source code
Functions are built in.
Also see
Manual: [M-5] select()
Help: [M-5] st_subview(); [M-2] op_colon, [M-2] subscripts; [M-4]
utility