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개)

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 CenterFile Exchange에서 Deep Learning Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by