| Sub Vectors |
VL provides the following functions for accessing subregions of vectors and matrices:
Vec[fd] sub(const Vec[fd] &v, Int start, Int length); Vec[fd] first(const Vec[fd] &v, Int length); Vec[fd] last(const Vec[fd] &v, Int length);
SubMat[fd] sub(const Mat[fd] &m, Int top, Int left, Int height, Int width = 1); SubMat[fd] sub(const Mat[fd] &m, Int rows, Int cols); SubVec[fd] col(const Mat[fd] &m, Int i); SubVec[fd] diag(const Mat[fd] &m, Int diagNum);
The utility of these functions is best illustrated with some examples:
u = sub(v, 2, 4); // return the 4 elements of v starting at element 2. u = first(v, 2); // return the first 2 elements of v. u = last(v, 2); // return the last 2 elements of v. v = m[i]; // extract row i of m v = col(m, i); // extract column i of m v = diag(m); // extract main diagonal of m v = diag(m, i) // extract diagonal starting on column i v = diag(m, -i) // extract diagonal starting on row i n = sub(m, 2, 3); // returns the upper-left 2 rows and three columns of m. n = sub(m, i, j, 2, 3); // returns the 2 rows and 3 columns of m starting at // row i, column j.Warning: remember that indexing is 0-based in VL, so row 2 above refers to the third row from the top of the matrix, and so on.
The subvector and submatrix types returned by the sub(), col()
and diag() functions can, in addition to being assigned to normal vectors
as above, also be assigned to:
diag(m) = diag(m) * 2.0; // multiply diagonal elements of m by 2 sub(m, 2, 2) = sub(m, 2, 2) + Matf(2, 2, vl_1); // add 1 to each of the upper-left 2 x 2 // elements of m.Warning: The standard in-place operations are not defined on submatrix regions, so the following will not work:
diag(m) *= 2.0; sub(m, 2, 2) += Matf(2, 2, vl_1);
|
|