Borrar filtros
Borrar filtros

I want to do random swapping in row vector and i know the number of swap to be performed.If row vector x=[1 2 3 4 5 6] the output vector should have unique element with fixed number of swap.

1 visualización (últimos 30 días)
swapping of vector should be random.

Respuesta aceptada

KL
KL el 10 de Mayo de 2017
x=[1 2 3 4 5 6]
nSwap = 2;
c = randi(length(x),[nSwap,2])
for i=1:nSwap
x(c(i,:)) = x(fliplr(c(i,:)));
end
x
  1 comentario
amit chatterjee
amit chatterjee el 22 de Mayo de 2017
If z is another vector i.e z=[1 1 1 0 1 0] so number of swap is no. of ones in z divided by 2 (when no. of one is odd then plus one divide by 2)and swapping in x element should be such that whenever the value of z is zero on that index element of x should not swapped otherwise x element will be swapped.The value of z change after each iteration and all the element is x should be unique.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.

Etiquetas

Aún no se han introducido etiquetas.

Community Treasure Hunt

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

Start Hunting!

Translated by