Nested for loops without repetition

2 次查看(过去 30 天)
Hi, I have the following problem. Supose I have shirts of 10 diferent colors , and I need to combine the shirts with a maximum number of 8 and only two colors without repetition. For example , I can have 2 blue shirts+3 black shirts but i don't need the 3 black shirts+2 blue combination because is repeated. I was trying to do this by using 4 for loops as follows
Colors(:,1)={'B';'BK';'R';'G';'M';'C';'Y';'O';'V';'GR'};
Info=struct;
Info.Combination{1,1}='Number of shirts Color1';
Info.Combination{1,2}='Number of shirts Color2';
Info.Combination{1,3}='Color1';
Info.Combination{1,4}='Color2';
CA=2; %Counter of combinations
MaxShirt=8; %Maximum number of shirts in the combination
for k=1:size(Colors,1)
CB=1; %Counter for the first color of shirts
CB2=1; %Counter for the second color of shirts
for ii=1:size(Colors,1)
for l=1:MaxShirt
for iii=1:MaxShirt-1
if CB+CB2>MaxShirt
break
end
if k==ii %Only one color of shirts
Info.Combination{CA,1}=CB+CB2; %Amount of first shirts
Info.Combination{CA,2}='NA'; %Does not apply
Info.Combination{CA,3}=Colors(k); %Amount of first shirts
Info.Combination{CA,4}='NA'; %Does not apply
else %Two colors of shirts
Info.Combination{CA,1}=CB; %Amount of first shirts
Info.Combination{CA,2}=CB2; %Amount of second shirts
Info.Combination{CA,3}=Colors(k); %Amount of first shirts
Info.Combination{CA,4}=Colors(ii); %Amount of second shirts
end
CB2=CB2+1;
CA=CA+1;
end
CB2=1;
CB=CB+1;
end
CB=1;
end
end
By doing that I have two problems of repetition. The first one is the repetition, as i mentioned at before. The second one appears because some combinations are the same, for example 2 blue shirts + 3 black shirts and some rows ahead the same combination 2 blue shitrs + 3 black shirts appears again.

采纳的回答

Bruno Luong
Bruno Luong 2020-8-1
编辑:Bruno Luong 2020-8-1
I won't fix your code with 4/5 nested for-loops and if conditions and ....
I give you an alternative code. It requires a function allVL1 that you gen get from here
Colors(:,1)={'B';'BK';'R';'G';'M';'C';'Y';'O';'V';'GR'};
MaxShirt=8;
maxk = 2;
Combination = [];
for k=1:maxk
% FEX file: all-permutations-of-integers-with-sum-criteria
% https://www.mathworks.com/matlabcentral/fileexchange/17818
n = allVL1(k,MaxShirt-k,'<=')+1;
c = nchoosek(Colors(:)',k);
p = size(n,1);
q = size(c,1);
[P,Q] = ndgrid(1:p,1:q);
N = n(P(:),:);
C = c(Q(:),:);
N = num2cell(N);
N(:,end+1:maxk) = {'NA'};
C(:,end+1:maxk) = {'NA'};
Combination = [Combination; [N, C]];
end
  3 个评论
Bruno Luong
Bruno Luong 2020-8-1
编辑:Bruno Luong 2020-8-1
Here is the modification with your new requirements. Usually I don't do this kind of adaptation.
People asking questions rarely think that the solution might be completely different when the specification changes.
I really dislike questions that have been not correctly specified at the start. ;-(
Colors(:,1)={'B';'BK';'R';'G';'M';'C';'Y';'O';'V';'GR'};
MinShirt=2;
MaxShirt=8;
MaxDist = 2;
maxk = 2;
Combination = [];
for k=1:maxk
% FEX file: all-permutations-of-integers-with-sum-criteria
% https://www.mathworks.com/matlabcentral/fileexchange/17818
n = allVL1(k,MaxShirt-k*MinShirt,'<=')+MinShirt;
c = nchoosek(1:length(Colors),k);
c(c(:,end)-c(:,1) > MaxDist,:) = [];
c = Colors(c);
p = size(n,1);
q = size(c,1);
[P,Q] = ndgrid(1:p,1:q);
N = n(P(:),:);
C = c(Q(:),:);
N = num2cell(N);
N(:,end+1:maxk) = {'NA'};
C(:,end+1:maxk) = {'NA'};
Combination = [Combination; [N, C]];
end
Juan Ruiz Osorio
Juan Ruiz Osorio 2020-8-1
编辑:Juan Ruiz Osorio 2020-8-1
Thank you Bruno, this code works perfectly. I understand what you said about posting new problems after an original question and I will take in count for future occasions. What happened was that I had a solution for the distance between colors using the nested for loops but I could not aply it to your code . The minimum amount of shirts was something that I forgot, sorry about that.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by