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 日

0 投票

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