problem using counter in a for loop

1 回表示 (過去 30 日間)
Mary
Mary 2013 年 10 月 15 日
コメント済み: Mary 2013 年 10 月 16 日
I'm doing some practice problems for experience and can't get past an error with my counter with this function. What am I missing here?
Thanks in advance
function x = triangle(n)
i = 1;
for i:n-1
x(i) = i+x(i+1);
i = i+1
end
end

採用された回答

Jos (10584)
Jos (10584) 2013 年 10 月 15 日
編集済み: Jos (10584) 2013 年 10 月 15 日
There are at least two issues with this code:
function x = triangle(n)
i = 1;
for i:n-1
x(i) = i+x(i+1); % (1) BOTH X(i) and X(i+1) ARE UNKNOWN, SO IT ERRORS
i = i+1 % (2) THIS HAS NO EFFECT
end
end
As for issue 2, try this (first think would you think will be displayed ...)
for i=1:10 ;
disp(i)
i = i + 7 ;
disp (i)
end
  1 件のコメント
Mary
Mary 2013 年 10 月 16 日
Thank you!

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

その他の回答 (1 件)

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 10 月 15 日
What is this?
x(i) = i+x(i+1);
x is unkown
  1 件のコメント
Mary
Mary 2013 年 10 月 16 日
You're right... Thanks for the help.

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

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by