subscripted assignment dimension mismatch. for loop

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

댓글 수: 1

M
M 2017년 12월 4일
편집: M 2017년 12월 5일
What is the expected result ?

댓글을 달려면 로그인하십시오.

답변 (1개)

Guillaume
Guillaume 2017년 12월 5일
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.

카테고리

도움말 센터 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2017년 12월 4일

답변:

2017년 12월 5일

Community Treasure Hunt

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

Start Hunting!

Translated by