could anyone help me to solve the issue in the following code

code:
X=[3]
for t=1:length(X)
A = partitions([X(t)])
for d=1:length(A)
for e=1:length(A{d})
C=A{d}{e}
end
end
end
If i run the code,it executes and gives the result.
C = 1 2 3
C = 1 2
C = 3
C = 1 3
C = 2
C = 1
C = 2 3
C = 1
C = 2
C = 3
But i want to have C should not display more than 2 numbers,which means
C should display
C = 1 2
C = 3
C = 1 3
C = 2
C = 1
C = 2 3
C = 1
C = 2
C = 3
by not considering C=1 2 3.
Could anyone please help me on this.

 Accepted Answer

X=[3]
for t=1:length(X)
A = partitions([X(t)])
for d=1:length(A)
if length(A{d})>1
for e=1:length(A{d})
C=A{d}{e}
end
end
end
end

17 Comments

ok. But what i actually need is how to check the size of e for each length d.
If size of e is more than 2 then that array should be ignored.
If size is less than or equal to 2 it needs to be displayed.Could you please help me on this for the code given below.
X=[3]
for t=1:length(X)
A = partitions([X(t)])
for d=1:length(A)
for e=1:length(A{d})
C=A{d}{e}
end
end
end
X=[3]
for t=1:length(X)
A = partitions([X(t)])
for d=1:length(A)
for e=1:length(A{d})
if length(A{d}{e}) <= 2
C=A{d}{e}
end
end
end
end
ok. But it doesnt remove the entire array,even if any one of the group size is more than 2.
what i actually need is if an array is having three groups in it,and if any one array size is more than two then the entire array should be removed. could you please help me on this.
X=[3]
for t=1:length(X)
A = partitions([X(t)])
for d=1:length(A)
if length(A{d}) ~= 3 || all(cellfun(@length,A{d}{e})<=2)
for e=1:length(A{d})
C=A{d}{e}
end
end
end
end
If i run the above code its giving error stating
Error using cellfun
Input #2 expected to be a cell array, was double instead.
Error in trial (line 6)
if length(A{d}) ~= 3 || all(cellfun(@length,A{d}{e})<=2)
Could you please help me on this.
X=[3]
for t=1:length(X)
A = partitions([X(t)])
for d=1:length(A)
if length(A{d}) ~= 3 || all(cellfun(@length,A{d})<=2)
for e=1:length(A{d})
C=A{d}{e}
end
end
end
end
the code executes. but the command if length(A{d}) ~= 3 all(cellfun(@length,A{d})<=2) has no impact on it. I am getting the output similar when i remove or include the line.
@ Walter Roberson:
Maybe for clarification:
length(A{d}) for the partition code gives the number of subsets in which the set {1,2,3} is partitioned. That means if the partition is {1 2 3}, length(A{d}) equals 1, if the partition is {1}{2}{3}, length(A{d}) equals 3 etc. So I think the if-condtion
if (length(A({d})>1
is correct for what the OP wanted to achieve.
@ Jaah Navi:
What exactly do you mean by
But what i actually need is how to check the size of e for each length d.
s i agree with you with respect to length(A{d}) which gives the number of subsets in which the set is partitioned.But i want to have the output in such a way that if anyone of the subset size present in partition is greater than 2 then the entire partition should be ignored.
But that's what the statement
if (length(A({d})>1
does. It ignores the trivial partition {1 2 3}.
Best wishes
Torsten.
the code executes and gives the below result
X=[3];
for t=1:length(X)
A = partitions([X(t)])
for d=1:length(A)
for e=1:length(A{d})
C=A{d}{e}
end
end
end
end
A =
{1x1 cell}
{1x2 cell}
{1x2 cell}
{1x2 cell}
{1x3 cell}
The 5 partitions of set {1 2 3}:
{1 2 3} - {1x1 cell}
{1 2} {3} -{1x2 cell}
{1 3} {2} - {1x2 cell}
{1} {2 3} - {1x2 cell}
{1} {2} {3} - {1x3 cell}
I actually want to have the output of partition containing subsets who size is less than 2. for example if one of the partition has two subsets of different sizes {1 2 3 4} {5 6},the entire partition should be removed eventhough one of the subset is equal to 2.
Given the set {1 2 3 4}, which partitions do you want to have as output ?
Consider command line X=[4] with respect to the above mentioned code,which gives the result
A = {1x1 cell}
{1x2 cell}
{1x2 cell}
{1x2 cell}
{1x3 cell}
{1x2 cell}
{1x2 cell}
{1x3 cell}
{1x2 cell}
{1x2 cell}
{1x3 cell}
{1x3 cell}
{1x3 cell}
{1x3 cell}
{1x4 cell}
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}
Among the 15 partitions i need to have only 10 partitions
{1 2} {3 4}
{1 2} {3} {4}
{1 3} {2 4}
{1 3} {2} {4}
{1 4} {2 3}
{1} {2 3} {4}
{1 4} {2} {3}
{1} {2 4} {3}
{1} {2} {3 4}
{1} {2} {3} {4}
as these partitions containting group size is not greater than 2.
where as the remaining 5 partitions to be ignored are
{1 2 3 4}
{1 2 3} {4}
{1 2 4} {3}
{1 3 4} {2}
{1} {2 3 4} as any one of the groups in each partition has size greater than 2.so the entire partition should be removed.
Then this code should work:
X=[4]
for t=1:length(X)
A = partitions([X(t)])
for d=1:length(A)
take_partition = 1;
for e=1:length(A{d})
if length(A{d}{e})>2
take_partition = 0;
break
end
end
if take_partition == 1
for e=1:length(A{d})
C = A{d}{e}
end
end
end
end
thanks.it works.Could you please help me how to remove the partitions which contain {3},{4} and {3 4} before checking with respect to length.
Or with Bruno's elegant solution:
A = partitions(4);
B = A(cellfun(@(p) all(cellfun('length',p)<=2),A));
partdisp(B)
Best wishes
Torsten.

Sign in to comment.

More Answers (2)

if length(A{d}) <= 2
Before the inner for loop. Make sure that you put in the matching end statement for the if

9 Comments

i tried with the above command in the code:
X=[3]
for t=1:length(X)
A = partitions([X(t)])
for d=1:length(A)
for e=1:length(A{d})
if length(A{d}) <= 2
C=A{d}{e}
end
end
end
end
but unable to get the result.Could you please help me on this.
I said before the inner for loop, not inside the inner for loop. In other words the if needs to be after the first for but before the second for.
ok
code:
X=[3]
for t=1:length(X)
A = partitions([X(t)])
for d=1:length(A)
if length(A{d}) <= 2
for e=1:length(A{d})
C=A{d}{e}
end
end
end
end
But still i am getting the length more than 2 to get displayed.with respect to the result already mentioned i should not get C= 1 2 3 to get displayed in the result as it is having more than 2 numbers.
I also tried with the following code:
clc;
X=[3]
for t=1:length(X)
A = partitions([X(t)])
for d=1:length(A)
if length(A{d}) <= 2
end
for e=1:length(A{d})
C=A{d}{e}
end
end
end
But still C= 1 2 3(which is more than 2 numbers) is getting displayed.Could you please help on this.

@jaah navi: your code:

for t=1:length(X)         % <- first FOR
A = partitions([X(t)])
for d=1:length(A)         % <- second FOR
if length(A{d}) <= 2      % <- the IF statement

Walter Roberson wrote: "...the if needs to be after the first for but before the second for."

Question: Is the IF after the first FOR?

Answer: Yes.

Question: Is the IF before the second FOR?

Answer: No. You put it in the wrong location.

If i put after the first for

 X=[3]
  for t=1:length(X)
  A = partitions([X(t)])
  if   length(A{d}) <= 2
  end
  for d=1:length(A) 
  for e=1:length(A{d}) 
  C=A{d}{e}
  end
  end
  end

I am getting the following result

X =
       3
A = 
      {1x1 cell}
      {1x2 cell}
      {1x2 cell}
      {1x2 cell}
      {1x3 cell}
C =
       1     2     3
C =
       1     2
C =
       3
C =
       1     3
C =
       2
C =
       1
C =
       2     3
C =
       1
C =
       2
C = 
     3

In that result i am getting C= 1 2 3 which should be present in the output.

@jaar navi: you have been using MATLAB for more than a year, so what do you expect this to achieve?:

if   length(A{d}) <= 2
end

An IF statement with nothing in it, does that really seem useful to you? Do you imagine that Walter Roberson wanted you to put an empty IF statement into your code, one that does absolutely nothing?

I am sure that you are capable of thinking about this, if you actually tried... Hint: you were told to put the IF between the two FOR loops... think about where the other END should go. Experiment! Your computer will not explode if you experiment.

i dont want to consider the length.I want to consider the size of e.If size of e is more than 2 it should not appear in the output.

@jaah navi: and you are too busy to read the length documentation, or actually try the code that Walter Roberson told you to use?

Sign in to comment.

Using this FEX to illustrate, but you can similarly remove loop with your partition function.
A = SetPartition(4);
B = A(cellfun(@(p) all(cellfun('length',p)<=2),A));
DispPartObj(B)
The 10 partition(s) are:
{1 2} {3 4}
{1 2} {3} {4}
{1 3} {2 4}
{1 3} {2} {4}
{1 4} {2 3}
{1} {2 3} {4}
{1 4} {2} {3}
{1} {2 4} {3}
{1} {2} {3 4}
{1} {2} {3} {4}

2 Comments

With respect to the code given by torsten
X=[4]
for t=1:length(X)
A = partitions([X(t)])
for d=1:length(A)
take_partition = 1;
for e=1:length(A{d})
if length(A{d}{e})>2
take_partition = 0;
break
end
end
if take_partition == 1
for e=1:length(A{d})
C = A{d}{e}
end
end
end
end
It executes and gives the result.
How to execute the code by omitting the partitions which contains {3 4},{3},{4} before checking with respect to length. Could you please help me on this.
If you have question to Torsten and his code, please move the comment under his answer.

Sign in to comment.

Categories

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

Tags

Asked:

on 13 Sep 2018

Edited:

on 28 Oct 2018

Community Treasure Hunt

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

Start Hunting!