How to find subsets of a set or a column

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

Walter Roberson
Walter Roberson 2013년 3월 18일
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 2013년 3월 20일
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.-

댓글을 달려면 로그인하십시오.

 채택된 답변

Cedric
Cedric 2013년 3월 20일
편집: Cedric 2013년 3월 20일

1 개 추천

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

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'

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 App Building에 대해 자세히 알아보기

질문:

2013년 3월 18일

Community Treasure Hunt

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

Start Hunting!

Translated by