Indexed Assignment
In MATLAB, indexed assignment enables you to modify specific elements of an array. This example shows how to use indexed assignment to modify vector elements, perform scalar expansion, and enable array growth. Assigning new values to an existing array without indexing overwrites the entire array. However, if you assign new values using indexing, MATLAB converts the data type of the new values to match the data type of the existing array. If the data types are not compatible, the assignment results in an error.
Modifying Array Elements Using Indexing
You can modify elements of an existing array by assigning new values to specific elements. To do this, select the elements of an existing array by using an indexing expression on the left side of the assignment operator =
, and assign new values on the right side of this operator.
Modify Vector Elements
Create a 1-by-4 vector of zeros.
v = zeros(1,4)
v = 1×4
0 0 0 0
Replace the second, third, and fourth elements of v
with 10, 15, and 20, respectively. Specify the indices of the elements you want to replace on the left side of the assignment operator and the new values on the right side.
v(2:4) = [10 15 20]
v = 1×4
0 10 15 20
Perform Scalar Expansion
In most cases, the number of elements on the right side should match the number of indexed elements on the left. However, MATLAB supports scalar expansion, where it expands a single scalar value on the right side to replace multiple elements on the left.
For example, replace the first, second, and third elements of v
with 30.
v(1:3) = 30
v = 1×4
30 30 30 20
Enable Array Growth
When you assign new values to an existing array by specifying indices outside the current bounds of the array, MATLAB also automatically grows the array. In this process, MATLAB expands the array size to accommodate the new values and appends these values to the array. When growing a numeric array, MATLAB fills any unspecified elements with 0.
For example, expand the vector v
by assigning new values to the sixth and eighth elements. MATLAB expands the size of v
to 1-by-8 and assigns the new values 2 and 3 to the sixth and eighth elements, respectively, while setting the fifth and seventh elements to 0.
v([6 8]) = 2:3
v = 1×8
30 30 30 20 0 2 0 3
To expand a matrix, specify two subscript indices for the row location and the column location of the matrix that lie outside the bounds of the current matrix size. For example, create a 2-by-2 matrix.
A = [2 3; -3 2]
A = 2×2
2 3
-3 2
Expand the matrix A
to 4-by-4 by assigning a new value to the element in the 4,4 position of A
. Here, MATLAB assigns the new value 5 to the element in the 4,4 position and sets the unspecified elements in the expanded array to 0.
A(4,4) = 5
A = 4×4
2 3 0 0
-3 2 0 0
0 0 0 0
0 0 0 5
Overwriting Arrays and Data Type Conversion
If you assign new values to an array without using indexing, MATLAB replaces the existing array with a new one constructed from the provided values. MATLAB discards the previous state of the original array, including the array size, data type, and any other attributes, effectively resetting the variable name to store the newly assigned array. If you assign new values to an array using indexing, MATLAB attempts to convert the values to the data type of the existing array.
Replace Entire Array
Create a 3-by-2 matrix A
that contains complex values. Check the existing size, class, and attributes of this matrix by using the whos
function. The matrix data type is the default double
type with a complex
attribute.
A = [pi 3; 1i 0; 1-2i 5]
A = 3×2 complex
3.1416 + 0.0000i 3.0000 + 0.0000i
0.0000 + 1.0000i 0.0000 + 0.0000i
1.0000 - 2.0000i 5.0000 + 0.0000i
whos A
Name Size Bytes Class Attributes A 3x2 96 double complex
If you assign a new value to A
without indexing, the assignment re-creates A
.
A = int16([1 2; 3 4])
A = 2x2 int16 matrix
1 2
3 4
A
is no longer a matrix of type double
, but a 2-by-2 matrix of type int16
(16-bit signed integer) without any attributes. Check the existing size, class, and attributes of A
.
whos A
Name Size Bytes Class Attributes A 2x2 8 int16
Convert Data Types with Indexed Assignment
If you perform an indexed assignment to an existing array, MATLAB keeps the previous state of the array whenever possible. MATLAB converts the new values on the right side of the assignment operator to the existing data type of the array on the left side. If the data type is not convertible, the assignment results in an error.
For example, assign the value 2.8 to the first row and column element of the previous matrix A
.
A(1,1) = 2.8
A = 2x2 int16 matrix
3 2
3 4
Here, MATLAB converts the assigned value to the existing int16
data type of A
, where the value 2.8 is rounded to the nearest integer, which is 3. Check that A
retains its size, class, and attributes after this assignment.
whos A
Name Size Bytes Class Attributes A 2x2 8 int16
See Also
colon
| createArray
| ones
| zeros
| reshape