Next: , Previous: , Up: Vector/Matrix Functions   [Contents][Index]

9.3 Extracting Vector Elements

The v r (calc-mrow) [mrow] command extracts one row of the matrix on the top of the stack, or one element of the plain vector on the top of the stack. The row or element is specified by the numeric prefix argument; the default is to prompt for the row or element number. The matrix or vector is replaced by the specified row or element in the form of a vector or scalar, respectively.

With a prefix argument of C-u only, v r takes the index of the element or row from the top of the stack, and the vector or matrix from the second-to-top position. If the index is itself a vector of integers, the result is a vector of the corresponding elements of the input vector, or a matrix of the corresponding rows of the input matrix. This command can be used to obtain any permutation of a vector.

With C-u, if the index is an interval form with integer components, it is interpreted as a range of indices and the corresponding subvector or submatrix is returned.

Subscript notation in algebraic formulas (‘a_b’) stands for the Calc function subscr, which is synonymous with mrow. Thus, ‘[x, y, z]_k’ produces ‘x’, ‘y’, or ‘z’ if ‘k’ is one, two, or three, respectively. A double subscript (‘M_i_j’, equivalent to ‘subscr(subscr(M, i), j)’) will access the element at row ‘i’, column ‘j’ of a matrix. The a _ (calc-subscript) command creates a subscript formula ‘a_b’ out of two stack entries. (It is on the a “algebra” prefix because subscripted variables are often used purely as an algebraic notation.)

Given a negative prefix argument, v r instead deletes one row or element from the matrix or vector on the top of the stack. Thus C-u 2 v r replaces a matrix with its second row, but C-u -2 v r replaces the matrix with the same matrix with its second row removed. In algebraic form this function is called mrrow.

Given a prefix argument of zero, v r extracts the diagonal elements of a square matrix in the form of a vector. In algebraic form this function is called getdiag.

The v c (calc-mcol) [mcol or mrcol] command is the analogous operation on columns of a matrix. Given a plain vector it extracts (or removes) one element, just like v r. If the index in C-u v c is an interval or vector and the argument is a matrix, the result is a submatrix with only the specified columns retained (and possibly permuted in the case of a vector index).

To extract a matrix element at a given row and column, use v r to extract the row as a vector, then v c to extract the column element from that vector. In algebraic formulas, it is often more convenient to use subscript notation: ‘m_i_j’ gives row ‘i’, column ‘j’ of matrix ‘m’.

The v s (calc-subvector) [subvec] command extracts a subvector of a vector. The arguments are the vector, the starting index, and the ending index, with the ending index in the top-of-stack position. The starting index indicates the first element of the vector to take. The ending index indicates the first element past the range to be taken. Thus, ‘subvec([a, b, c, d, e], 2, 4)’ produces the subvector ‘[b, c]’. You could get the same result using ‘mrow([a, b, c, d, e], [2 .. 4))’.

If either the start or the end index is zero or negative, it is interpreted as relative to the end of the vector. Thus ‘subvec([a, b, c, d, e], 2, -2)’ also produces ‘[b, c]’. In the algebraic form, the end index can be omitted in which case it is taken as zero, i.e., elements from the starting element to the end of the vector are used. The infinity symbol, inf, also has this effect when used as the ending index.

With the Inverse flag, I v s [rsubvec] removes a subvector from a vector. The arguments are interpreted the same as for the normal v s command. Thus, ‘rsubvec([a, b, c, d, e], 2, 4)’ produces ‘[a, d, e]’. It is always true that subvec and rsubvec return complementary parts of the input vector.

See Selecting Sub-Formulas, for an alternative way to operate on vectors one element at a time.

Next: Manipulating Vectors, Previous: Building Vectors, Up: Vector/Matrix Functions   [Contents][Index]