An "if-loop" and "for-loop" problem
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Angus Wong
le 20 Juin 2018
Commenté : Angus Wong
le 20 Juin 2018
Please help. I am trying to make a nested for-loop with a if-loop inside as follows:
S=0;
for i=1:10
for j=(i+1):10
for k=(j+1):10
for l=(k):10
if i<j && j<k && k<l && range(l)~=0 && range(k)~=0 && range(j)~=0 && range(i)~=0
S=S+i*j*k*l;
end
end
end
end
end
This code doesn't work. Any way to solve this?
The purpose of the range function I used is that if the upper limit and the lower limit of the counter are the same, then S remains unchanged.
The result I want is something like S=1x2x3x4+1x2x3x5+1x2x3x6+.....+7x8x9x10
0 commentaires
Réponse acceptée
the cyclist
le 20 Juin 2018
I haven't tried to run your code, but I'm going to guess here that the expressions like
range(i)
are not doing what you expect. i is just a scalar, so range(i) is always going to be equal to zero. The range command returns the difference between the maximum and the minimum value of the input.
I'm not quite sure what you intended there.
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur MATLAB Compiler 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!