MATLAB Answers

Why do I get the "Subscripted assignment dimension mismatch" error message?

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. 

1 Answer

Answer by MathWorks Support Team on 12 Jun 2017 at 4:00
Edited by MathWorks Support Team on 12 Jun 2017 at 20:23
 Accepted Answer

This error occurs when you attempt to assign elements to an existing array, but the size of the variable you are trying to assign is not compatible with the existing array. For example, the following code snippet produces this error:

A = [1 2 3; 4 5 6];
B = [7 8 9 10];
A(2,:) = B

In this case, 'A' has size 2x3, that is, 2 rows and 3 columns. The vector 'B' has size 1x4, 1 row and 4 columns. The assignment statement attempts to replace the second row of 'A' with the row vector 'B', but this is not possible because 'B' has 4 columns, and 'A' only has 3 columns. In order to assign to the second row of 'A', you must use a vector with 3 columns. For example,

B = [11 12 13];
A(2,:) = B

For more information on indexing in MATLAB, see the following documentation page:

https://www.mathworks.com/help/matlab/math/matrix-indexing.html

  0 Comments

Log in to comment.


Discover MakerZone

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

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!