An "if-loop" and "for-loop" problem
조회 수: 2 (최근 30일)
이전 댓글 표시
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
채택된 답변
the cyclist
2018년 6월 20일
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.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 MATLAB Compiler에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!