MATLAB Answers

subset algorithm! How to follow?

3 views (last 30 days)
% 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

Accepted Answer

Rishik Ramena
Rishik Ramena on 5 Nov 2020
Edited: Rishik Ramena on 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

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by