Main Content

Detailed Rules for Indexed Assignment

In MATLAB®, indexed assignment allows you to modify specific elements of an array, perform scalar expansion, and enable array growth, as discussed in Indexed Assignment.

This topic discusses how MATLAB handles indexed assignment when modifying numeric arrays, including how MATLAB processes repeated indices in indexed assignment. To perform an indexed assignment, such as A(J,K,…) = B, MATLAB follows specific rules to ensure compatibility between the dimensions of the source and target arrays.

Rules for Indexed Assignment

When you perform an indexed assignment A(J,K,…) = B to modify the elements of an existing array A, MATLAB follows specific rules. Here, the subscripts J, K, and so on, are indexing expressions that can be scalars, vectors, or arrays. If you use colon operators as subscripts, then each single colon represents all elements in the dimension indicated by the subscript location. The colon is equivalent to using 1:end for that dimension. Each colon is treated as a scalar or vector depending on the dimension size.

The rules for the indexed assignment A(J,K,…) = B are:

  • The number of nonscalar subscripts specified for A equals the number of dimensions of B whose sizes do not equal 1.

  • The order and length of all nonscalar subscripts specified for A match the order and length of dimensions of B whose sizes do not equal 1.

You can also perform an indexed assignment A(J,K,…) = C(M,N,…) for existing arrays A and C. First, MATLAB resolves the indices M, N, and so on, to access the elements of C, following the rules discussed in Array Indexing and Detailed Rules for Array Indexing. Then, MATLAB performs the indexed assignment A(J,K,…) = C(M,N,…) using the same rules as for A(J,K,…) = B, where the order and length of all nonscalar subscripts specified for A must match the order and length of nonscalar subscripts specified for C.

The following examples demonstrate these rules.

Create a 3-by-3 matrix of zeros. Create a 2-by-2 matrix to use as the new values in the assignment.

A = zeros(3,3);
B = [1 2; 3 4];

Modify the elements of A that are located in the second to third rows and second to third columns by replacing these elements with B. You can perform this assignment because the elements of A that you want to replace have the size 2-by-2, which is the same size as B.

A(2:3,2:3) = B
A = 3×3

     0     0     0
     0     1     2
     0     3     4

You can also specify additional trailing dimensions of size 1 when modifying the elements of A, such as A(2:3,2:3,1,1). For more information, see Arrays with Dimensions of Size 1. Here, you can perform this assignment because the indices with value 1 are the trailing dimensions of A with length 1.

A(2:3,2:3,1,1) = B
A = 3×3

     0     0     0
     0     1     2
     0     3     4

Next, create a 2-by-4-by-1-by-3 array and a 1-by-3 row vector.

A = rand(2,4,1,3);
B = 1:3;

Replace the three elements of A(2,1:3,1,2) with B. You can perform this assignment because there is one nonscalar subscript of length 3 specified for A, and the length of the vector B is also 3.

A(2,1:3,1,2) = B
A = 
A(:,:,1,1) =

    0.8147    0.1270    0.6324    0.2785
    0.9058    0.9134    0.0975    0.5469


A(:,:,1,2) =

    0.9575    0.1576    0.9572    0.8003
    1.0000    2.0000    3.0000    0.1419


A(:,:,1,3) =

    0.4218    0.7922    0.6557    0.8491
    0.9157    0.9595    0.0357    0.9340

Finally, create a 4-by-2-by-4 array. Also create a 1-by-4-by-3 array of integers.

A = rand(4,2,4);
B = randi([1 8],1,4,3);

Replace the twelve elements of A(1:4,2,2:4) with B. The order and length of all nonscalar subscripts specified for A match the order and length of dimensions of B whose sizes do not equal 1. The assignment is valid because the left side of the statement (ignoring the scalar subscript 2) uses a 4-element subscript followed by a 3-element subscript, and the dimensions of B whose sizes do not equal 1 have lengths of 4 and 3.

A(1:4,2,2:4) = B
A = 
A(:,:,1) =

    0.6787    0.6555
    0.7577    0.1712
    0.7431    0.7060
    0.3922    0.0318


A(:,:,2) =

    0.2769    8.0000
    0.0462    3.0000
    0.0971    5.0000
    0.8235    2.0000


A(:,:,3) =

    0.4387    7.0000
    0.3816    3.0000
    0.7655    5.0000
    0.7952    6.0000


A(:,:,4) =

    0.7094    8.0000
    0.7547    8.0000
    0.2760    5.0000
    0.6797    2.0000

Indexed Assignment with Repeated Indices

To access and modify specific elements of an existing array, you can specify indices to locate each element. In most cases, you can specify indices that are unique to the location of each element. For example, create a 1-by-3 vector. Modify the first and third elements to have new values of 2 and 3. The result is a 1-by-3 vector with the value 2 in the first element and the value 3 in the third element.

A = [-10 0 10];
id = [1 3];
A(id) = [2 3]
A = 1×3

     2     0     3

If you use repeated indices in a subscript vector to access array elements and assign new values to them, then MATLAB follows the order of the provided indices from left to right (for a row vector) or top to bottom (for a column vector). If you specify repeated indices in a subscript array, MATLAB processes the indices as if they are in a single column created by appending the columns of the array from left to right. This is because MATLAB stores an array in memory column by column, proceeding from left to right.

For example, create a 1-by-3 vector and specify the element locations to replace as 1, 3, 3, and 1 in a subscript row vector.

A = [-10 0 10];
id = [1 3 3 1];

Assign the new values 2, 3, 4, and 5 to the elements at these specified locations. MATLAB replaces the first element with 2, replaces the third element with 3, replaces the third element again with 4, and finally replaces the first element again with 5.

A(id) = [2 3 4 5]
A = 1×3

     5     0     4

See Also

Topics