Subscripted assignment dimension mismatch.

조회 수: 1 (최근 30일)
Hosein Jeddi
Hosein Jeddi 2021년 2월 15일
답변: Walter Roberson 2021년 2월 15일
hello
a=[0 1;0 0];
b=[0;1];
q1=10;
q2=1;
r=1;
N=3;
S(3)=1;
Q=[q1 q2;q2 q1];
for k=2:-1:0
K(k,:)=inv((b'*S(k+1)*b)+r)*b'*S(k+1)*a;
S(k,:)=(a'*S(k+1)*(a-b*K(k)))+Q;
end
Subscripted assignment dimension mismatch.
Error in Untitled452 (line 13)
S(k,:)=(a'*S(k+1)*(a-b*K(k)))+Q;

답변 (1개)

Walter Roberson
Walter Roberson 2021년 2월 15일
S(3)=1;
S did not exist before. The above is equivalent to
S = zeros(1,3);
S(1,3) = 1;
Then
Q=[q1 q2;q2 q1];
That is 2 x 2
S(k,:)=(a'*S(k+1)*(a-b*K(k)))+Q;
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.)

카테고리

Help CenterFile Exchange에서 Simulink에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by