swaping 2 random numbers from array

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

 采纳的回答

ij = randperm(length(x)-1,2)+1;
x(ij) = x(flip(ij));

3 个评论

thank you, how would i store this new order in a different varaiable?
Simply copy it
y = x;
ij = randperm(length(y)-1,2)+1;
y(ij) = y(flip(ij));

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by