Borrar filtros
Borrar filtros

building all such vectors?

2 visualizaciones (últimos 30 días)
Mahdi
Mahdi el 7 de Ag. de 2013
How to build all vector with A elements equal to 1, B elements equal to 2, and C elements equal to 3 ?
I'm looking for an algorithm or a function building all such vectors.

Respuesta aceptada

Roger Stafford
Roger Stafford el 7 de Ag. de 2013
This is a problem in combinations, not permutations, and can be performed using matlab's 'nchoosek' function. Note that the number of possible combinations in your problem is
(A+B+C)!/A!/B!/C! = (A+B+C)!/A!/(B+C)! * (B+C)!/B!/C!
The code:
p1 = nchoosek(1:A+B+C,B+C); % Choose B+C indices out of 1:A+B+C
p2 = nchoosek(1:B+C,C); % Choose C indices out of 1:B+C
m = size(p1,1);
n = size(p2,1);
V = ones(m*n,A+B+C); % Start with all 1's
for ix = 1:m
V((1:n)+n*(ix-1),p1(ix,:)) = 2; % Reset B+C of them to 2's
for jx = 1:n
V(jx+n*(ix-1),p1(ix,p2(jx,:))) = 3; % Of these, reset C to 3's
end
end
  2 comentarios
Roger Stafford
Roger Stafford el 8 de Ag. de 2013
Editada: Jan el 8 de Ag. de 2013
I had forgotten that I previously answered an essentially equivalent question to this at the 'Answers' site:
I used a slightly different technique there which you might be interested in trying.
Jan
Jan el 8 de Ag. de 2013
@Roger: The similarity between the questions is impressive or surprising. I prefer the other implementation due to its simplicity.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Labeling, Segmentation, and Detection en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by