subscripted assignment dimension mismatch. for loop

1 view (last 30 days)
In my code, i need to update the value of A with the calculated value C, but its not working The code is:
clc
clear
A=[1 0 1 0;0 1 0 1;0 0 1 0;0 0 0 1];
B=[1;-1;-0.05;0.05];
for i=1:10
C(i)=A*B
%update value
A=C(i)
end

Answers (1)

Guillaume
Guillaume on 5 Dec 2017
It's really not clear what you want to do. There are many issues with the short bit of code you've shown:
  • Issue 1: What is the point of the loop? You have
for i = 1:10
something = A*B
end
Nothing in the loop depends on i. Hence you're basically doing the exact same thing 10 times, each time completely overwriting what was calculated the previous step.
  • Issue 2: Trying to assign a vector to a scalar
C(i) = A*B;
A*B is a 4x1 vector. Unless C is some obscure class, C(i) is a 1x1 scalar. You can't stuff 4 elements in a single slot. That's why you get the subscripted assignment dimension mismatch error. It's unclear what you're really trying to do here.
  • Issue 3: Your update completely overwrite the whole of A
A = C(i);
As said, I'm not sure what you wanted C to contain. C(i) is scalar, but maybe you wanted it to contain the 4x1 vector. Either way, you're replacing a 4x4 matrix by either a 1x1 or a 4x1 array. There's no update there.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!