subset algorithm! How to follow?

1 visualización (últimos 30 días)
주몽 고
주몽 고 el 2 de Nov. de 2020
Editada: Rishik Ramena el 5 de Nov. de 2020
% code
n = input('n:') % n=[1,2,3,4,5,6]
j = input('j:') % j=3
subset = [1:j] %subset = [1,2,3]
h = j+1. %h=4
found = [false*j]
while h>1 and found == false;
h = h-1. %h=3
if subset(h)<n+h-j
found(h) = true
end;
end;
if found(h) == false;
subset;
end;
else
subset(h) = subset(h) + 1
for k = h+1:j %k = [5:3]??
subset(k) = subset(k-1) + 1
subset
end;
end;
I coded with MATLAB. However,there are areas that do not work well. How do I follow this algorithm? please

Respuesta aceptada

Rishik Ramena
Rishik Ramena el 5 de Nov. de 2020
Editada: Rishik Ramena el 5 de Nov. de 2020
A few points to note regarding the implementation of the algorithm:
  • step 12 could be implemented by using a while loop.
  • And the algorithm does not specify that FOUND needs to be an array.
  • Also do note the termination of the algorithm at step 7.
n = input('n:'); % n=4
j = input('j:'); % j=2
subset = 1:j % subset = [1,2]
while(true) %% step 3
h = j+1; % h=3
found = false;
while (h>1) && (found == false)
h = h-1; %h=2
if subset(h)<(n+h-j)
found = true;
end
end
if found == false
return % exit (step 7)
else
subset(h) = subset(h) + 1;
for k = h+1:j % (step 9)
subset(k) = subset(k-1) + 1;
end
subset(1:j) % step 11
end % goto step 3
end

Más respuestas (0)

Community Treasure Hunt

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

Start Hunting!

Translated by