MATLAB: How to find subsets of a set or a column

find subsets from setsetsubsets

I have a column named income having values:
high
high
high
medium
low
low
low
medium
low
medium
medium
medium
high
medium
Now i want subsets from this set excluding powerset and null set
i.e.
{low,medium},{low,high},{medium,high},{low},{medium},{high}.
Thanks.

Best Answer

A solution could be ..
>> data = {'high', 'high', 'high', 'medium', 'low', 'low', 'low', 'medium', ...
'low', 'medium', 'medium', 'medium', 'high', 'medium'} ;
>> dataItems = unique(data)
dataItems =
'high' 'low' 'medium'
>> n = numel(dataItems) ;
>> combs = arrayfun(@(k)dataItems(combntns(1:n,k)), 1:n, 'UniformOutput', false)
>> combs{1} % Combinations of 1 item.
ans =
'high' 'low' 'medium'
>> combs{2} % Combinations of 2 items.
ans =
'high' 'low'
'high' 'medium'
'low' 'medium'
>> combs{3} % Combination of all items = full set.
ans =
'high' 'low' 'medium'
To understand the internals, look at what we get when we evaluate
>> ids = arrayfun(@(k)combntns(1:n,k), 1:n, 'UniformOutput', false)
ids =
[3x1 double] [3x2 double] [1x3 double]
>> ids{1}
ans =
1
2
3
>> ids{2}
ans =
1 2
1 3
2 3
>> ids{3}
ans =
1 2 3
so, what we do when we compute combs is to use these ids to index relevant items in dataItems.
Hope it helps!
PS: this is the power set minus the empty set. When you say that you don't want the "power set", do you mean actually that you don't want the set of all elements? If so, you can just replace n by n-1 in the call to ARRAYFUN:
>> combs = arrayfun(@(k)dataItems(combntns(1:n,k)), 1:n-1, 'UniformOutput', false)
combs =
{1x3 cell} {3x2 cell}
>> combs{1}
ans =
'high' 'low' 'medium'
>> combs{2}
ans =
'high' 'low'
'high' 'medium'
'low' 'medium'