フィルターのクリア

Choose pairs of integers without repeating this selection in a loop

2 ビュー (過去 30 日間)
Ehsan
Ehsan 2014 年 4 月 28 日
コメント済み: Ehsan 2014 年 4 月 28 日
Hi. I want select randomly two number by 'randsample' or 'randperm' function in a loop (for intermediate recombination in evolutionary strategy matlab code). But I want any pair of numbers can be selected only once. For example if selected [1 2], in next time [1 2] or [2 1] are not selected. please help me. Thanks

採用された回答

Image Analyst
Image Analyst 2014 年 4 月 28 日
Seems really, really easy. Did you try it? This is what I got:
theNumbers = randperm(50) % Whatever number you want.
for k = 1 : 2 : length(theNumbers)/2
selection = theNumbers(k:k+1) % Report this selection to command window.
end
  3 件のコメント
Image Analyst
Image Analyst 2014 年 4 月 28 日
I have no idea what that means. First of all, I have selection, not selected. Secondly it's a numerical array, not a cell array. And third, you said "any pair of numbers can be selected only once " which seems to be a direct contradiction of saying it still "has a chance of being selected."
Ehsan
Ehsan 2014 年 4 月 28 日
for example:
A = {1,2,....,10};
Loop 1 : { 1 & 5 } selected
Loop 2 : { 5 & 7 } selected ( {5} has chance for selected )
Loop 3 : { 5 & 1 } selected >> #error >> next loop
Loop 4 : { 7 & 2 } selected
.
.
.

サインインしてコメントする。

その他の回答 (1 件)

Geoff Hayes
Geoff Hayes 2014 年 4 月 28 日
Hi Ehsan,
One option is to keep track of a list of all the possible choices for your recombination, randomly select a pair of indices into that list, remove those two from your list of choices and then repeat for the next random selection (the two that you removed previously will no longer be in the list to choose from):
N = 26; % the max number of choices
recomChoices = 1:N; % the vector of choices
rndPairIndcs = randperm(length(recomChoices),2); % the randomly chosen indices for recombination
rndPair = recomChoices(rndPairIndcs); % the numbers/ids for recombination (i.e. 1,2)
recomChoices(rndPairIndcs) = []; % remove that pair from the list
Hope that this helps!
Geoff

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by