Add a command for unique permutations

1 visualización (últimos 30 días)
bettythedigitalhero
bettythedigitalhero el 4 de Dic. de 2019
Comentada: bettythedigitalhero el 4 de Dic. de 2019
At this moment I have function which creates a matrix of n rows and Nvar columns. I want to add to the if-line that each permutation should be unique.
E.g. when I enter InitialPopulation(60,3); I get a lot of the same permutations but I want him to make a list of the unique ones.
This is my code:
function InitPop = InitialPopulation(n,Nvar)
InitPop = zeros(n, Nvar);
for index = 1:n
test = 0;
while test ~= 1
test = 0;
InitPop(index,:) = randperm(Nvar);
%random permutations of the vertices
if InitPop(index, 1) == 1
test = 1;
%making sure that the permutation starts with the first vertex
end
end
end
end

Respuestas (1)

JESUS DAVID ARIZA ROYETH
JESUS DAVID ARIZA ROYETH el 4 de Dic. de 2019
you can make
list = unique (InitialPopulation (60,3), 'rows')
but it would no longer be 60 in size
On the other hand if you want to get all the permutations you can simply do:
perms (1: 3)

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by