MATLAB: Swap 3 random elements in an array

arraysexchangeMATLABmatrixpermutationswapvector

How do I randomly swap three elements in an array? The element at selected position one will be moved to selected position two, the previous element at selected position two will be moved to position three, and the element at position three will be moved to selected position one.
For example: a=[4 2 3 1 5 7 6] would become anew=[4 2 6 1 3 7 5]
Thank you

Best Answer

Use randperm to select 3 elements (without replacement) from your vector.
a = [4 2 3 1 5 7 6]
ind = randperm(numel(a), 3)
a(ind) = a(ind([3 1 2]))