# Indistinguishable Objects to Distinguishable Boxes
# The number of different ways to distribute Nballs indistinguishable balls into
# Nboxes distinguishable boxes is C(Nboxes+Nboxes-1,Nboxes-1).
# For example, 5 balls into 3 boxes can be done in these C(7,2) = 21 ways:
# Number of balls
Nballs <- 10
# Number of boxes
Nboxes <- 3
# The number of different ways to distribute n indistinguishable balls into
# k distinguishable boxes is C(n+k-1,k-1).
# nb<-choose(N+nbjour-1,nbjour-1)=dim(tb)[1]
# divers<-matrix(rep(0, nbjour*nb), ncol=nbjour)
# generate all possible positions of the boundaries
xx <- combn(Nballs+Nboxes-1, Nboxes-1)
# compute the number of balls in each box
a <- cbind(0, diag(Nboxes)) - cbind(diag(Nboxes), 0)
tb <- t(a %*% rbind(0, xx, Nballs+Nboxes) - 1)
Aucun commentaire:
Enregistrer un commentaire