trouble with complicated loop

1 回表示 (過去 30 日間)
Rock Rocky
Rock Rocky 2014 年 6 月 10 日
コメント済み: Rock Rocky 2014 年 6 月 10 日
Hello guys would u please help me in this question Let’s say the length of x=1000, I want to make a loop to calculate the value of A For I=1:x A=(1/I)*(x+1) %when the value of x=1 % when the value of x=2 the formula should be A=(1/I)*(x+2) + ((1/I-1)*(x+1)) % when the value of x=2 the formula should be A=((1/I)*(x+3)) + ((1/I-1)*(x+2) +(1/I-3)* (x+1))
. . . . and so on my question is how can I make this loop in a right way in order to calculate the value of A starting from x=1 till x=1000 thanks in advance
  2 件のコメント
Roger Wohlwend
Roger Wohlwend 2014 年 6 月 10 日
It seems that there are some typos in the formulas. Please make sure that the equations are correct - especially the one for x = 2;
Rock Rocky
Rock Rocky 2014 年 6 月 10 日
Ok let me put it in this way, I want to make a loop in order to add the current state with the previous state for example. M=1000; for i=1:M A=i*(x(i)); % this formula will be for M=1 but when M=2 the formula will be like this A=(i*(x(i))+((i-1)*(x(i-1)))and when M=3 the formula will be A=(i*(x(i))+((i-1)*(x(i-1)))+((i-2)*(x(i-2)) and so on till reach the maximum length of M which is 1000.

サインインしてコメントする。

採用された回答

David Sanchez
David Sanchez 2014 年 6 月 10 日
% You are playing with two differet indeces: _i_ and _M_.
% You have to fix _i_ and then perform the summation:
x=rand(10,1); % sample data
M=4; % choose your own M
i = M + 1; % i has to be greater than M. Otherwise an error will occur.
A = 0; % initialize the summation
for k=1:M
A= A + (i-(k-1))*x(i-(k-1));
end
  1 件のコメント
Rock Rocky
Rock Rocky 2014 年 6 月 10 日
Much thanks Mr David Sanchez I really appreciate your answer my best wishes to you.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by