How to use nested for loops?

4 vues (au cours des 30 derniers jours)
Yasmin Touly
Yasmin Touly le 10 Avr 2018
Commenté : Birdman le 10 Avr 2018
I am very new to MATLAB, and I have been trying to figure out how to use nested for loops correctly. I want to print out :
1
121
12321
1234321
123454321
what should I do if there are a varying number of columns? Any help would be greatly appreciated.
  3 commentaires
Yasmin Touly
Yasmin Touly le 10 Avr 2018
@Walter Roberson, since it is not an array, I am not sure what exactly I should do. I am lost.
Walter Roberson
Walter Roberson le 10 Avr 2018
for variable = something : something_else
for another_variable = thing2 : thing3
% do something involving variable and another_variable
end
end

Connectez-vous pour commenter.

Réponses (1)

Birdman
Birdman le 10 Avr 2018
Modifié(e) : Birdman le 10 Avr 2018
No need for nested for loops. Try this:
i=1;n=5;
while i<=n
fprintf('%d',[1:i-1 i:-1:1]);
fprintf('\n');
i=i+1;
end
  6 commentaires
Yasmin Touly
Yasmin Touly le 10 Avr 2018
@Birdman, it is, but I wanted to know how I can specifically use nested for loops for this particular question.
Birdman
Birdman le 10 Avr 2018
n=5;
for j=1:n
for i=1:j
end
fprintf('%d',[1:i-1 i:-1:1]);
fprintf('\n');
end

Connectez-vous pour commenter.

Catégories

En savoir plus sur Loops and Conditional Statements 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