フィルターのクリア

how to generate a random sequence by excluding random number for each trial?

15 ビュー (過去 30 日間)
Mudasir Ahmed
Mudasir Ahmed 2015 年 12 月 30 日
コメント済み: Mudasir Ahmed 2015 年 12 月 30 日
hi
i want to generate two random numbers in between sequence 1 to 5. like 1 and 3, or 2 and 5, or 1 and 4. but the sequence is subject to one condition. which is like, generate two random numbers from 1 to 5 by excluding one random number. for e.g.
if x=3
it means generate two numbers in between [1 2 4 5] but not 3 . 3 is excluded from the choice. the result must be like
1 and 4. or 2 and 5 etc
Kindly help me :)
with best regards mudasir ahmed

採用された回答

Image Analyst
Image Analyst 2015 年 12 月 30 日
I show both cases, where it's totally random so that the number may possibly repeat, and pseudo random where no repeat is allowed. Not sure which you wanted so I had to do both:
% Get two numbers in range with possible repeats.
iMin = 1;
iMax = 5;
% Get 100 numbers between iMin and iMax
r = randi([iMin, iMax], 100);
% Get rid of one certain number
numberToRemove = 3;
r(r==numberToRemove) = []; % r is smaller now.
% Now get two numbers from that by taking the first two.
r = r(1:2)
% Get two numbers in range without any repeats.
iMin = 1;
iMax = 5;
numToTake = 2; % Must be less than iMax - iMin
% Get (iMax-iMin) numbers between iMin and iMax
r = randperm(iMax-iMin) + iMin;
% Get rid of one certain number
numberToRemove = 3;
r(r==numberToRemove) = []; % r is smaller now.
% Now get two numbers from that by taking the first two.
r = r(1:2)
  1 件のコメント
Mudasir Ahmed
Mudasir Ahmed 2015 年 12 月 30 日
thank you so much sir, i need second one :)
withe best regards mudasir

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

タグが未入力です。

Community Treasure Hunt

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

Start Hunting!

Translated by