Subscripted assignment dimension mismatch.

2 Ansichten (letzte 30 Tage)
Hosein Jeddi
Hosein Jeddi am 15 Feb. 2021
Beantwortet: Walter Roberson am 15 Feb. 2021
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;

Antworten (1)

Walter Roberson
Walter Roberson am 15 Feb. 2021
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.)

Kategorien

Mehr zu Entering Commands finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by