This is an old version, view current version.

6.10 Slicing and blocking functions

Stan provides several functions for generating slices or blocks or diagonal entries for matrices.

6.10.1 Columns and rows

vector col(matrix x, int n)
The n-th column of matrix x
Available since 2.0

row_vector row(matrix x, int m)
The m-th row of matrix x
Available since 2.0

The row function is special in that it may be used as an lvalue in an assignment statement (i.e., something to which a value may be assigned). The row function is also special in that the indexing notation x[m] is just an alternative way of writing row(x,m). The col function may not, be used as an lvalue, nor is there an indexing based shorthand for it.

6.10.2 Block operations

6.10.2.1 Matrix slicing operations

Block operations may be used to extract a sub-block of a matrix.

matrix block(matrix x, int i, int j, int n_rows, int n_cols)
Return the submatrix of x that starts at row i and column j and extends n_rows rows and n_cols columns.
Available since 2.0

The sub-row and sub-column operations may be used to extract a slice of row or column from a matrix

vector sub_col(matrix x, int i, int j, int n_rows)
Return the sub-column of x that starts at row i and column j and extends n_rows rows and 1 column.
Available since 2.0

row_vector sub_row(matrix x, int i, int j, int n_cols)
Return the sub-row of x that starts at row i and column j and extends 1 row and n_cols columns.
Available since 2.0

6.10.2.2 Vector and array slicing operations

The head operation extracts the first \(n\) elements of a vector and the tail operation the last. The segment operation extracts an arbitrary subvector.

vector head(vector v, int n)
Return the vector consisting of the first n elements of v.
Available since 2.0

row_vector head(row_vector rv, int n)
Return the row vector consisting of the first n elements of rv.
Available since 2.0

array[] T head(array[] T sv, int n)
Return the array consisting of the first n elements of sv; applies to up to three-dimensional arrays containing any type of elements T.
Available since 2.0

vector tail(vector v, int n)
Return the vector consisting of the last n elements of v.
Available since 2.0

row_vector tail(row_vector rv, int n)
Return the row vector consisting of the last n elements of rv.
Available since 2.0

array[] T tail(array[] T sv, int n)
Return the array consisting of the last n elements of sv; applies to up to three-dimensional arrays containing any type of elements T.
Available since 2.0

vector segment(vector v, int i, int n)
Return the vector consisting of the n elements of v starting at i; i.e., elements i through through i + n - 1.
Available since 2.0

row_vector segment(row_vector rv, int i, int n)
Return the row vector consisting of the n elements of rv starting at i; i.e., elements i through through i + n - 1.
Available since 2.10

array[] T segment(array[] T sv, int i, int n)
Return the array consisting of the n elements of sv starting at i; i.e., elements i through through i + n - 1. Applies to up to three-dimensional arrays containing any type of elements T.
Available since 2.0