Effacer les filtres
Effacer les filtres

combination of integer numbers

2 vues (au cours des 30 derniers jours)
Nasser Hakami
Nasser Hakami le 16 Nov 2019
Commenté : Nasser Hakami le 19 Nov 2019
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 commentaires
Walter Roberson
Walter Roberson le 16 Nov 2019
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
Nasser Hakami le 18 Nov 2019
thanks. however i couldn't manage to do it

Connectez-vous pour commenter.

Réponse acceptée

the cyclist
the cyclist le 16 Nov 2019
I would download John D'Errico's partitions function, and then I believe your answer is
p = partitions(15,[1 1 1 1])
  5 commentaires
the cyclist
the cyclist le 18 Nov 2019
I don't think an easier example was needed, but I can verify that
partitions(4,[1 1 1])
gives the same answer (not necessarily in the same row order).
I'm not sure why you couldn't run the code. Did you do the following?
  • go to the link I uploaded
  • click on the download button to get the files
  • unzip the files
  • put the partitions.m file in your path so that you can run it
Nasser Hakami
Nasser Hakami le 19 Nov 2019
thanks a lot
it is working perfect
the problem that i just copied the code from webpage and i didnt download file

Connectez-vous pour commenter.

Plus de réponses (1)

Walter Roberson
Walter Roberson le 18 Nov 2019
[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);

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by