Asked by MathWorks Support Team
on 27 Feb 2012

Why do I get the following error message sometimes:

??? Subscripted assignment dimension mismatch.

??? In an assignment A(I) = B, the number of elements in B and

I must be the same.

Answer by MathWorks Support Team
on 27 Feb 2012

Accepted answer

The above error message can be recieved when you are attempting to assign more elements to a section of a matrix or vector than that section can hold.

These two error messages occur when you

i) Use a colon operator to specify an entire row or column in the destination matrix.

ii) Use linear indexing with an incorrectly sized index array.

Common Causes include:

You are attempting to replace a section of one matrix with another and the size of the index vector or vectors used to specify the section of the destination matrix to be replaced does not match that of the source matrix. Alternatively, you are attempting to assign values to individual elements using row and column indices, which allocates a submatrix section of the original matrix.

Solution:

Stop MATLAB on the line where the error occurs. Verify that the size of the index array or arrays agrees with the size of the array you are attempting to assign to the destination matrix. You should verify that the computations that created the index array and the vector or matrix to be assigned are correct. An unexpected size may indicate that the computations that created the matrix were incorrect.

Finally, you may want to use the REPMAT function to expand a smaller matrix to fill a large block, make use of scalar expansion to fill the region to which you are trying to assign, or use the SUB2IND or IND2SUB function to generate an appropriate index vector or set of index vectors.

Example demonstrating this error:

'AssignmentSizeMismatch.m'

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi test

Learn moreOpportunities for recent engineering grads.

Apply Today
## 0 Comments