Borrar filtros
Borrar filtros

An "if-loop" and "for-loop" problem

2 visualizaciones (últimos 30 días)
Angus Wong
Angus Wong el 20 de Jun. de 2018
Comentada: Angus Wong el 20 de Jun. de 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

Respuesta aceptada

the cyclist
the cyclist el 20 de Jun. de 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.

Más respuestas (0)

Categorías

Más información sobre MATLAB Compiler en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by