|
Contents | Previous | Next | Subchapters |
| Syntax |
vector(indices)matrix(row indices, :)matrix(:, column indices)matrix(row indices, column indices)
|
| See Also | vector elements , matrix elements , base , non-sequential access |
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.
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).
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 ]
}
= )
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 ]