フィルターのクリア

How to write coding for the following problem

1 回表示 (過去 30 日間)
ESWARA MOORTHI
ESWARA MOORTHI 2012 年 7 月 4 日
How to calculate the following loop
y(1)=x;
for i=1:3
y(i+1)=z(i)+y(i);
i=i+1;
end
Ans=z(1)+z(2)+z(3)+3*x
  1 件のコメント
Kevin Claytor
Kevin Claytor 2012 年 7 月 4 日
You don't need the "i=i+1" statement - the increment is contained next to the for statement;
for i = start:increment:end
disp(i);
i = i+1;
end
So for start = 1, increment = 1, end = 3 you would get "1 2 3" For start = 2, increment = 2, end = 7 you would get "2 4 6" Your added "i=i+1" affects everything after it, but then the value for i is re-written when the for loop begins again. For example, try this;
for i = 1:3
disp(i);
i = i+1;
disp(i)
end
Should give you the output; "1 2 2 3 3 4"

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

回答 (2 件)

Luffy
Luffy 2012 年 7 月 4 日
編集済み: Luffy 2012 年 7 月 4 日
Assuming z is a vector of length 3,x is scalar
y = zeros(1,4)
y(1)= x;
for i = 1:3
y(i+1) = y(i)+z(i);
end
y(4) + 2*x % How did you get 3*x,do you want y(4)/ans you specified

F.
F. 2012 年 7 月 4 日
Hello, I don't understand how you can make i=i+1 in a loop on i !! ;-)
When I see your code, I can write this:
y(1) = x
y(2) = z(1) + y(1) = z(1) + x
y(3) = z(2) + y(2) = z(1) + z(2) + x
y(4) = z(3) + y(3) = z(1) + z(2) + z(3) + x
so:
y(n+1) = sum( z(1:n) ) + x
and not what you wrote:
Ans=z(1)+z(2)+z(3)+3*x
Where is the problem ....

カテゴリ

Help Center および File ExchangeMultidimensional Arrays についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by