Effacer les filtres
Effacer les filtres

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 vue (au cours des 30 derniers jours)
swapping of vector should be random.

Réponse acceptée

KL
KL le 10 Mai 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 commentaire
amit chatterjee
amit chatterjee le 22 Mai 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.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange

Tags

Aucun tag saisi pour le moment.

Community Treasure Hunt

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

Start Hunting!

Translated by