Subscripted assignment dimension mismatch.

조회 수: 1(최근 30일)
Hosein Jeddi
Hosein Jeddi 2021년 2월 15일
답변: Walter Roberson 2021년 2월 15일
a=[0 1;0 0];
Q=[q1 q2;q2 q1];
for k=2:-1:0
Subscripted assignment dimension mismatch.
Error in Untitled452 (line 13)


Walter Roberson
Walter Roberson 2021년 2월 15일
S did not exist before. The above is equivalent to
S = zeros(1,3);
S(1,3) = 1;
Q=[q1 q2;q2 q1];
That is 2 x 2
You are adding Q, a 2 x 2. You wil either get an error from size mismatches, or else you will get a 2 x 2 output. Unless, that is, the expression happens before the Q happens to calculate something that is 2 x 2 x something or 1 x 2 by something or 2 x 1 x something, in which case you would get 2 x 2 x something as the size of the right hand side.
This (probably) 2 x 2 output is to be assigned to S(2,:) . S is currently 1 x 3, so that is an assignment to S(2, 1:3) which requires the right hand side to be a scalar, or a vector that is 3 elements long. But the right hand side is 2 x 2.
If you had initialized S as zeros(1,4) then the 2 x 2 on the right hand side would be the correct number of elements, but it would be the wrong shape to assign to a 1 x 4 output location.
If you are going to calculate a 2 x 2 on the right hand side, then the place you designate on the left has to be 2 x 2 (or 1 x 2 x 2, or 2 x 1 x 2, or other variations such as 2 x 1 x 1 x 1 x 1 x 2 -- the non-scalar dimensions have to match 2 x 2.)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by