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