combination of integer numbers
古いコメントを表示
I need to make distibution of 15 apples among 4 person [a b c d],
each person can have a value betwwen 0-15. and summation always (a+b+c+d=15)
so how can i generate the matrix for all possible combinantion
so i eapect matrix as
a = [1 14 0 0
0 0 0 15
3 6 2 4
...................
...................]
2 件のコメント
Walter Roberson
2019 年 11 月 16 日
One approach is to ndgrid() all possible combinations, and then check those to find the ones that add up to the right number.
Nasser Hakami
2019 年 11 月 18 日
採用された回答
その他の回答 (1 件)
Walter Roberson
2019 年 11 月 18 日
[Ag, Bg, Cg, Dg] = ndgrid(0:15);
Eg = [Ag(:), Bg(:), Cg(:), Dg(:)];
mask = sum(Eg,2) == 15;
selected = Eg(mask,:);
a = selected(:,1);
b = selected(:,2);
c = selected(:,3);
d = selected(:,4);
カテゴリ
ヘルプ センター および File Exchange で Standard File Formats についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!