필터 지우기
필터 지우기

how to add combinatorics function in matlab with n different objects divided into r groups and each group get at least 1 object

조회 수: 3 (최근 30일)
I have 7 machines which I want to group in 3 cells such that each cell contain atleast 1 machine the numeric formula for this is
3^732^7+3=21873(128)+3=1806
I want to generate a matrix with each combination and run loop over that. is there any way I can code this? for example the 1st array is 1 1 1 1 2 3 1 and running the 2nd time loop give another value for example 1 1 1 2 1 1 3

채택된 답변

Roger Stafford
Roger Stafford 2017년 1월 11일
C = zeros(r^n,n);
t = 0;
for k = 0:(r^n)-1
s = dec2base(k,r,n);
if length(unique(s))==r
t = t+1;
C(t,:) = s-0+1;
end
end
C = C(1:t,:);
  댓글 수: 3
Roger Stafford
Roger Stafford 2017년 1월 11일
편집: Roger Stafford 2017년 1월 11일
Using your values r = 3 and n = 7, the range of k = 0:(3^7)-1 is exactly the range of numbers that in base 3 can be represented as a 7-digit number:
0000000
0000001
0000002
0000010
....
2222222
There are 3^7 such base 3 numbers, and they are created by Matlab’s ‘dec2base’ function. See:
https://www.mathworks.com/help/matlab/ref/dec2base.html
The requirement “length(unique(s))==r” forces each of the three groups to have at least one digit representing it. (Actually the digits will be one below the corresponding group number.) Hence, for example, '0010121' is accepted but '0010111' is not.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Elementary Math에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by