MATLAB: 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.

swapping of vector should be random.

Best Answer

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