swaping 2 random numbers from array

1 view (last 30 days)
hello,
I have an array x = [1,(randperm(20)+1)]
but i would like to swap 2 random numbers of that array while keeping 1 as my first integer
please let me know how to go about that

Accepted Answer

Bruno Luong
Bruno Luong on 28 Apr 2022
ij = randperm(length(x)-1,2)+1;
x(ij) = x(flip(ij));
  3 Comments
Bruno Luong
Bruno Luong on 28 Apr 2022
Simply copy it
y = x;
ij = randperm(length(y)-1,2)+1;
y(ij) = y(flip(ij));

Sign in to comment.

More Answers (0)

Categories

Find more on Random Number Generation in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!