Using the C++ Math Library | ![]() ![]() |
Assigning to a Multiple Elements
Use a vector index to modify multiple elements in a matrix.
The colon()
index frequently appears in the subscript of the destination because it allows you to modify an entire row or column. For example, the code
A(2,colon()) = ramp(1,3);
replaces the second row of an M-by-3 matrix with the vector 1 2 3
. If we use the example matrix A
, A
is modified to contain:
1 4 7 1 2 3 3 6 9
You can also use a logical index to select multiple elements. For example, the assignment statement
A(A>5)= horzcat(17,17,17,17);
changes all the elements in A
that are greater than 5
to 17
:
1 4 17 2 5 17 3 17 17
![]() | Assigning to a Single Element | Assigning to a Subarray | ![]() |