HOW CAN I FIND THE SUBSET OF N-SET?

2 visualizzazioni (ultimi 30 giorni)
matlabmaster
matlabmaster il 30 Ott 2020
Commentato: Walter Roberson il 30 Ott 2020
Given S={1,2,3,4,5,6...................n}
Find the subset of S whose element has j without rand or function.
I want to find this answer with using only for,while,etc....
example) S={1,2,3,4}, j=2
ANSWER= {1 2} {1 3} {1 4} {2 3} {2 4} {3 4}
  3 Commenti
matlabmaster
matlabmaster il 30 Ott 2020
I want these result
if S=[1,2,3,4,5] and j=3.
1 2 3
1 2 4
1 2 5
1 3 4
1 3 5
1 4 5
2 3 4
2 3 5
2 4 5
3 4 5
i want these result at any S ! using input n and j!
NOT using etc nchoosek,randperm,etc.......

Accedi per commentare.

Risposte (2)

KSSV
KSSV il 30 Ott 2020
S=[1,2,3,4] ;
j=2 ;
iwant = nchoosek(S,j)

Walter Roberson
Walter Roberson il 30 Ott 2020
You would use as many positions as you wanted to select at the same time, and you would use the rule that the elements must be sorted -- so when you increment something to the left, have a pass back towards the right in which you set the entries to increasing integers, and re-increment if that does not fit. For example your limit is 6 and you are at 1 3 5 6 then "odometer" would roll to 1 3 5 7 carry left -> 1 3 6 1, fix up to right -> 1 3 6 (6+1->overflow) so 1 3 6 1 carry left again -> 1 3 (6+1->overflow) 1 so carry left on the overflow -> 1 4 1 1 now fix up to right -> 1 4 (4+1) (4+1+1) = 1 4 5 6 which is valid. (There would be better ways to implement this in practice.)
  1 Commento
Walter Roberson
Walter Roberson il 30 Ott 2020
If this Answer did not work for you, then it would be better if you were to comment as to what difficulties you ran into when using it. I cannot improve the answer if I do not know what went wrong for you.

Accedi per commentare.

Categorie

Scopri di più su MATLAB in Help Center e File Exchange

Prodotti


Release

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by