problem using counter in a for loop

1 vue (au cours des 30 derniers jours)
Mary
Mary le 15 Oct 2013
Commenté : Mary le 16 Oct 2013
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

Réponse acceptée

Jos (10584)
Jos (10584) le 15 Oct 2013
Modifié(e) : Jos (10584) le 15 Oct 2013
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 commentaire
Mary
Mary le 16 Oct 2013
Thank you!

Connectez-vous pour commenter.

Plus de réponses (1)

Azzi Abdelmalek
Azzi Abdelmalek le 15 Oct 2013
What is this?
x(i) = i+x(i+1);
x is unkown
  1 commentaire
Mary
Mary le 16 Oct 2013
You're right... Thanks for the help.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by