could anyone help me to solve the issue
1 次查看(过去 30 天)
显示 更早的评论
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}
0 个评论
采纳的回答
Bruno Luong
2018-10-28
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}
0 个评论
更多回答(1 个)
Walter Roberson
2018-10-28
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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!