I have a list $\ell$ of ordered subsets of $1,\dots,n$ with a maximum length, e.g. for $n = 4$ with maximum length 3 this list is of the form
{{}, {1}, {2}, {3}, {4}, {1,2}, {1,3}, {1,4}, {2,3}, {2,4}, {3,4}, {1,2,3}, {1,2,4}, {1,3,4}, {2,3,4}}
Given a number $m \leq n$ and a subset $s \subseteq \{1,\dots,n\}$ I want to efficiently replace the elements of this list $\ell$, based on the following conditions:
- if neither $m$ nor one of the numbers in $s$ is in the element replace the element by 5
- if $m$ is in the element but not one of the numbers in $s$ replace by 4
- if one or more of the numbers in $s$ but not $m$ is in the element replace by 3
- if both $m$ and one or more of the numbers in $s$ is in the element replace by 2
For the example given above and $m = 3, s = \{1,2\}$ this would result in a list:
{5, 3, 3, 4, 5, 3, 2, 3, 2, 3, 4, 2, 3, 2, 2}
My code below works and gives the right output, but I was wondering if someone might know a faster to do this? Any hint/help is welcome!
Input:
powerset = Subsets[Range[4], 3];
number1 = 3;
numbers2 = {1, 2};
Code:
vector = {MemberQ[#, number1], Length[Intersection[#, numbers2]]} & /@ powerset;
rules = {{False, 0} -> 5, {True, 0} -> 4, {False, _} -> 3, {True, _} -> 2};
vector = Fold[Replace[#1, #2, {1}] &, vector, rules]
Note: on the given example my code is not that slow, but it slows down when the input given is e.g.
powerset = Subsets[Range[200], 3];
and I want it to work faster for these larger instances.