How to find subsets of a set or a column

2 views (last 30 days)
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.
  2 Comments
Walter Roberson
Walter Roberson on 18 Mar 2013
Is it definitely a set? As in {high, high, high} is the same as {high} ? So it doesn't matter whether row 1's high or row 2's high is taken?
Amish
Amish on 20 Mar 2013
NO i don't want it like that... sorry i think i told u in wrong manner , its like if we have three different strings i.e {high,low,medium} then i want subsets in new cell i.e {low,medium},{low,high},{medium,high},{low},{medium},{high}. Another example: if i have a cell weather={strong,weak,low,high} then i want another cell containing different subsets of weather i.e weather_new = {{strong},{weak},{low},{high},{strong,weak},{weak,low},{low,high},{high,strong},{strong,weak,low},{weak,low,high},{low,high,strong}}; i want all the subsets of a set of column excluding null set {} and power set.-

Sign in to comment.

Accepted Answer

Cedric
Cedric on 20 Mar 2013
Edited: Cedric on 20 Mar 2013
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'
  1 Comment
Cedric
Cedric on 21 Mar 2013
It seems that you deleted a question while I was answering, so here was my answer:
The simplest is probably to use Andrei's solution (correcting the typo) and to evaluate:
>> [weather_new{:}]
If you want to modify my solution, you can replace the line that I gave for computing combs with the following
>> combs = arrayfun(@(k)reshape(dataItems(combntns(1:n,k)),1,[]), 1:n, ...
'UniformOutput', false) ;
and then evaluate
>> [combs{:}]
ans =
Columns 1 through 7
'high' 'low' 'medium' 'high' 'high' 'low' 'low'
Columns 8 through 12
'medium' 'medium' 'high' 'low' 'medium'

Sign in to comment.

More Answers (0)

Categories

Find more on MATLAB in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!