I want to do random swapping in row vector and i know the number of swap to be performed.If row vector x=[1 2 3 4 5 6] the output vector should have unique element with fixed number of swap.

1 次查看(过去 30 天)
swapping of vector should be random.

采纳的回答

KL
KL 2017-5-10
x=[1 2 3 4 5 6]
nSwap = 2;
c = randi(length(x),[nSwap,2])
for i=1:nSwap
x(c(i,:)) = x(fliplr(c(i,:)));
end
x
  1 个评论
amit chatterjee
amit chatterjee 2017-5-22
If z is another vector i.e z=[1 1 1 0 1 0] so number of swap is no. of ones in z divided by 2 (when no. of one is odd then plus one divide by 2)and swapping in x element should be such that whenever the value of z is zero on that index element of x should not swapped otherwise x element will be swapped.The value of z change after each iteration and all the element is x should be unique.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile 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