0

Given a set S=[a,b,c,v,f,d,e], we want to enumerate each possible non-empty subsets. Clearly there will be 2^7-1 such subsets. There ia a way of computing the subsets explicitly like this:-

enter image description here

One could map the above set S to a 7-bit string and then compute all possible 7-bit strings using the pseudo code above implemented in matlab. Each will correspond to a desired subset except the first one. Above pic is from lec. slides of Computer Science 226: Algorithms and Data Structures for Spring 2011.

I am looking for a MATLAB command that does this automatically. Otherwise what is the easiest way to enumerate all non-empty subsets in matlab with least coding required?

user_1_1_1
  • 903
  • 1
  • 12
  • 27
  • 1
    `dec2bin([1:2^7-1].')`? – beaker Jul 13 '17 at 20:01
  • Actually, you don't even need the transpose. `dec2bin(1:2^7-1)` – beaker Jul 13 '17 at 20:07
  • Possible duplicate of [Matlab: how to build a combination according to the logical 0 and 1 but by adding 100?](https://stackoverflow.com/questions/27825298/matlab-how-to-build-a-combination-according-to-the-logical-0-and-1-but-by-addin) – beaker Jul 13 '17 at 20:14

1 Answers1

2

Not sure how efficient it is, but it's super short:

combMat = (dec2bin(1:(2.^numel(S)-1)) == '1');

And each row is a logical index into S to create a set.

gnovice
  • 125,304
  • 15
  • 256
  • 359