MATLAB: Switch two random elements in a vector

arrayrandomrandpermvector

How do I randomly change two elements in a vector?
For example, I start with x1=randperm(42) Then I want x2 to equal x1, except with two of those numbers randomly switched
Thank you

Best Answer

>> x2 = x1;
>> v = randi(numel(x2),1,2);
>> x2(v) = x2(v([2,1]))
Related Question