An "if-loop" and "for-loop" problem
2 views (last 30 days)
Show older comments
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 Comments
Accepted Answer
the cyclist
on 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.
More Answers (0)
See Also
Categories
Find more on MATLAB Compiler in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!