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

조회 수: 5 (최근 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)

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

아직 태그를 입력하지 않았습니다.

Community Treasure Hunt

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

Start Hunting!

Translated by