Contents Previous Next Subchapters

Vector Indices
Syntax vector(indices)
matrix(row indices, :)
matrix(:, column indices)
matrix(row indicescolumn indices)
See Also vector elements , matrix elements , base , non-sequential access

Description
References the subvector consisting of the elements with the specified indices, or the submatrix consisting of the rows and columns with the specified indices, where indices, row indices and column indices are integer vectors (it does not matter if they are row or column vectors). (If row indices or column indices is real, or double-precision, it is converted to integer using the same convention as in the int function.)

The result of an assignment has the type that corresponds to coercion between the type of the left and right hand side of the assignment.

If either of the index vectors is real, or double-precision, it is interpreted as an integer vector using the same convention as in the int function.

Tutorial

Subvectors
You can use integer vector indices to access subvectors. If you enter
     x       = {5., 4., 3., 2.}
     indices = {1, 4}
     x(indices)
O-Matrix will respond
     {
     5
     2
     }
If you continue by entering
     x = x'
     x(indices)
O-Matrix will respond
     [ 5 , 2 ]
You can also use integer vector indices to assign values to a subvector. If you continue by entering
     x(indices) = [0., 0.]
     print type(x),  x
O-Matrix will respond
     real [ 0 , 4 , 3 , 0 ]
Note that the result is real because the original vector was integer and the subvector was real. In addition, the subvector x(indices) is a row vector because x is a row vector (it doesn't matter whether indices is a row or column vector).

Submatrices
You can also use integer vector indices to access submatrices. If you enter
     x     = {[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]}
     rows  = {1, 3}
     x(rows, :)
O-Matrix will respond
     {
     [ 1 , 2 , 3 , 4 ]
     [ 9 , 10 , 11 , 12 ]
     }
If you continue by entering
     cols = {2, 4}
     x(:, cols)
O-Matrix will respond
     {
     [  2 , 4 ]
     [  6 , 8 ]
     [ 10 , 12 ]
     }
If you then enter
     x(rows, cols)
O-Matrix will respond
     {
     [ 2 , 4 ]
     [ 10 , 12 ]
     }


You can also use integer vector indices to assign a value to a submatrix. If you continue the previous example by entering
     x(rows, cols) = {[0, -1.1], [-1.1, 0]}
     print x
O-Matrix will respond
     {
     [ 1 , 0 , 3 , -1.1 ]
     [ 5 , 6 , 7 , 8 ]
     [ 9 , -1.1 , 11 , 0 ]
     }
If you then enter
     x(rows, :) = {[4, 3, 2, 1], [12, 11, 10, 9]}
     print x
O-Matrix will respond
     {
     [ 4 , 3 , 2 , 1 ]
     [ 5 , 6 , 7 , 8 ]
     [ 12 , 11 , 10 , 9 ]
     }
If you continue by entering
     x(:, cols) = [{11, 6, 3}, {9, 8, 1}]
     print x
O-Matrix will respond
     {
     [ 4 , 11 , 2 , 9 ]
     [ 5 , 6 , 7 , 8 ]
     [ 12 , 3 , 10 , 1 ]
     }

Removing Matrix Elements
Normally the submatrix on the left side of an assignment operator ( = ) must have the same number of elements as the matrix on the right side of the assignment operator. There is an exception to this rule when the matrix on the right side of the assignment operator is an empty matrix . In this case, the elements corresponding to the submatrix specified by indices are removed from the matrix on the left side of the assignment operator. For example, if you enter
     x = [1, 2, 3, 4]
     k = [2, 3]
     x(k) = []
     print x
O-Matrix will respond
     [ 1 , 4 ]
If you enter
     x = { [ 1 , 2 ] , [ 3 , 4 ] }
     x(:, 1) = []
     print x
O-Matrix will respond
     {
     2
     4
     }
If you enter
     x = { [ 1 , 2 ] , [ 3 , 4 ] }
     x(1, :) = []
     print x
O-Matrix will respond
     [ 3 , 4 ]