Clear Filters
Clear Filters

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 view (last 30 days)
swapping of vector should be random.

Accepted Answer

KL
KL on 10 May 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 Comment
amit chatterjee
amit chatterjee on 22 May 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.

Sign in to comment.

More Answers (0)

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Tags

No tags entered yet.

Community Treasure Hunt

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

Start Hunting!