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