Placing 6 items in 3 different boxes

조회 수: 5 (최근 30일)
Sharada Premarathne
Sharada Premarathne 2018년 8월 3일
답변: Akira Agata 2018년 8월 10일
I need to group 6 items in different three boxes. Any combination shall be possible, even empty boxes are allowed. How can I write a code to find all the possible combinations?

답변 (1개)

Akira Agata
Akira Agata 2018년 8월 10일
Like this?
N1, N2 and N3 are possible combination of select k out of 6 items for 1st box, l out of 6-k items for 2nd box and 6-k-l out of 6-k-l items for 3rd box. Note that N3 is always 1.
Nall = 0;
for k = 0:6
N1 = nchoosek(6,k);
for l = 0:6-k
N2 = nchoosek(6-k,l);
N3 = nchoosek(6-k-l,6-k-l);
Nall = Nall + N1*N2*N3;
end
end
The answer is:
>> Nall
Nall =
729

카테고리

Help CenterFile Exchange에서 MATLAB Compiler SDK에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by