MATLAB: How to select two number randomly from an array and perform swapping between these two number

the size of array is about 50.

for example in array A=[1 2 3 5 6 9 10] two number selected randomly are 3 and 6 so after swapping array be like A=[1 2 6 5 3 9 10].

Best Answer

A=[1 2 3 5 6 9 10] ;
%%select two numbers and swap
%%using randsample
idx = randsample(1:length(A),2) ;
B = A ;
B(fliplr(idx)) = A(idx) ;
%%using randperm
idx = randperm(length(A),2) ;
B = A ;
B(fliplr(idx)) = A(idx) ;