Nested for loops without repetition
Ältere Kommentare anzeigen
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.
Akzeptierte Antwort
Weitere Antworten (0)
Kategorien
Mehr zu Resampling Techniques finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!