On Mon, 20 Aug 2012 12:22:35 -0700 (PDT), MD wrote:
> Hi All,
>
> I have the following question: is there any simple way in IDL which can find all the possible subsets of a vector of n elements starting from subsets with 2 elements until a final subset of n elements (last subset will have all the elements of the given vector). For example:
>
> let A = [1,2, 3, 4] be a given vector. The subsets would be: [1, 2], [1, 3], [1, 4], [2, 3], [2, 4], [3, 4], [1, 2, 3], [1, 2, 4], [1, 3, 4], [2, 3, 4], [1, 2, 3, 4]. Note that the subsets that have the same elements are assumed equal, although the elements have different sequence: for example [2, 3] and [3, 2] are the same.
>
> Any help is appreciated!
>
> MD
Let n be the number of elements of the given vector a. Then the number
of different subsets is 2^n-1. They can be calculated as follows:
a=[1,2,3,4]
n=n_elements(a)
for i=1,2^n-1 do print,a[where((i/2L^lindgen(n)) mod 2L eq 1L)]
Have fun, Heinz
|