This is an old version, view current version.

5.10 Slicing and blocking functions

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

5.10.1 Columns and rows

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

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

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.

5.10.2 Block operations

5.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.

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.

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.

5.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.

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

T[] head(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.

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

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

T[] tail(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.

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.

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.

T[] segment(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.