First of all, every matrix may be indexed as a matrix or a as a long vector consisting of the stacked columns of the matrix (corresponding to the usual vec(.) operator in the numerical linear algebra literature), i.e., for every matrix class, a matrix
A can be indexed via
- A(i,j): which returns the element corresponding to row i and column j. For most classes there are two version, one returning the reference to the element, so that it can be changed, too, and one const version, the only returns the value. For sparse matrices only the second version exists.
- A(k): if m is the row dimension of A, then A(k) adresses the same element as A(i,j) with i=km (=k mod m) and j=k/m. Again both versions, reference and value, exist for most classes.
- A[k]: same as A(k)
For most matrix classes it is possible to extract a single row or column or several rows and columns indexed by a given Indexmatrix ind via
- col(i): returns a column vector corresponding to column i
- row(i): returns a row vector corresponding to row i
- cols(ind): returns a new matrix containing the columns given by ind in this sequence
- rows(ind): returns a new matrix containing the rows given by ind in this sequence
Likewise most classes offer possibilities to delete a subset of columns/rows to insert single columns/rows, to concat matrices of appropriate sizes below or to the right, and to extract the main diagonal of the matrix with the operator diag(). For the class Matrix it is also possible to assign matrices to appropriately indexed submatrices or to extract upper or lower triangles starting with any diagonal, for details see the documentation there.
The content of two matrices A and B of the same type can always be exchanged with very litte constant cost by the friend function swap(A,B). It simply exchanges the vector pointing to the memory.
Every matrix class also offers direct access to its internal representation. Needless to say, that users should be extremely careful in making use of these methods.