user_1_1_1
user_1_1_1

Reputation: 923

Computing all N-bit strings in matlab through inbuilt command

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?

Upvotes: 0

Views: 54

Answers (1)

gnovice
gnovice

Reputation: 125864

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.

Upvotes: 2

Related Questions