How do I randomly change two elements in a vector?
For example, I start with x1=randperm(42) Then I want x2 to equal x1, except with two of those numbers randomly switched
Thank you

댓글 수: 1

Stephen23
Stephen23 2017년 11월 4일
@Jermaine Garcia: what happens if the two random picked elements randomly happen to pick the same element? Or, to put it another way, do you want to exclude the possibility that no elements are swapped?

댓글을 달려면 로그인하십시오.

 채택된 답변

Stephen23
Stephen23 2017년 11월 4일
편집: Stephen23 2017년 11월 4일

0 개 추천

>> x2 = x1;
>> v = randi(numel(x2),1,2);
>> x2(v) = x2(v([2,1]))

추가 답변 (0개)

카테고리

도움말 센터 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

질문:

2017년 11월 4일

편집:

2017년 11월 4일

Community Treasure Hunt

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

Start Hunting!

Translated by