An "if-loop" and "for-loop" problem
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Angus Wong
am 20 Jun. 2018
Kommentiert: Angus Wong
am 20 Jun. 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 Kommentare
Akzeptierte Antwort
the cyclist
am 20 Jun. 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.
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu MATLAB Compiler finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!