Clear Filters
Clear Filters

could anyone help me to solve the issue

1 view (last 30 days)
how to remove the partitions which contains {3},{4} and {3 4} in the following partitions of set{1 2 3 4} The 15 partitions of set {1 2 3 4}:
{1 2 3 4}
{1 2 3} {4}
{1 2 4} {3}
{1 2} {3 4}
{1 2} {3} {4}
{1 3 4} {2}
{1 3} {2 4}
{1 3} {2} {4}
{1 4} {2 3}
{1} {2 3 4}
{1} {2 3} {4}
{1 4} {2} {3}
{1} {2 4} {3}
{1} {2} {3 4}
{1} {2} {3} {4}

Accepted Answer

Bruno Luong
Bruno Luong on 28 Oct 2018
Using this FEX to illustrate, but you can similarly remove loop with your partition function.
A = SetPartition(4);
NotWanted = {[3] [4] [3 4]};
matchfun = @(s) any(cellfun(@(x) isequal(s,x), NotWanted));
b = cellfun(@(c) any(cellfun(matchfun,c)), A);
A(b) = [];
partdisp(A)
You'll get the following
The 5 partitions of set {1 2 3 4}:
{1 2 3 4}
{1 3 4} {2}
{1 3} {2 4}
{1 4} {2 3}
{1} {2 3 4}

More Answers (1)

Walter Roberson
Walter Roberson on 28 Oct 2018
Learn how to use for loops and while loops. Learn how to index cell arrays. Learn how to use if statements. Learn how to use "break". Learn how to add an element to an existing array or remove an element from an existing array.
Hint: isequal() can compare arrays of different lengths.
Important programming advice:
Get the code to work first. Even if it means using for loops nested 8 deep. Get it to work.
If you don't know a fancy short way to write the code, write it the long way. Get it to work. Don't worry about making it fancy unless it is taking all day to execute.
As it is now, you are being held up for months at a time trying to find fancy ways to write code that will be executed a small number of times, and that is blocking you from making progress on your project.
Get the code work first. You can always go back to make it nicer looking after you have gotten right through to the end of the task.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!