subset algorithm! How to follow?

2 vues (au cours des 30 derniers jours)
주몽 고
주몽 고 le 2 Nov 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

Réponse acceptée

Rishik Ramena
Rishik Ramena le 5 Nov 2020
Modifié(e) : Rishik Ramena le 5 Nov 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

Plus de réponses (0)

Catégories

En savoir plus sur Simulink dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by