Swap 3 random elements in an array
이전 댓글 표시
How do I randomly swap three elements in an array? The element at selected position one will be moved to selected position two, the previous element at selected position two will be moved to position three, and the element at position three will be moved to selected position one.
For example: a=[4 2 3 1 5 7 6] would become anew=[4 2 6 1 3 7 5]
Thank you
채택된 답변
추가 답변 (1개)
KALYAN ACHARJYA
2018년 8월 20일
편집: KALYAN ACHARJYA
2018년 8월 20일
% I have tried this only swap two elements or even numbers, definite swap with each other is only possible in even numbers.
a=randperm(7)
idx=randperm(3);
a(idx)=a(fliplr(idx))
I have tried in another way, you can do that
a=randperm(7)
n=randi(5);
new_array=a(:,:);
new_array([n n+1 n+2])=a([n+2 n n+1])
댓글 수: 4
Amine Ne
2018년 8월 20일
KALYAN ACHARJYA
2018년 8월 20일
Wait I am doing, there is little an issue.
KALYAN ACHARJYA
2018년 8월 20일
I have added (Edited answer) another way, pls try, if OK pls confirm it
카테고리
도움말 센터 및 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!