Random numbers without repetition

조회 수: 6 (최근 30일)
joey
joey 2011년 3월 31일
X=1 2 3 4 5 6 7 8 9 10
Y=[1 3 4;1 6 9;6 7 8] o Y=[1 3 5;6 8 6;1 5 3] x Like this numbers of each row should be generated randomly with no repetition And no rows should have numbers
Help!
  댓글 수: 2
Jan
Jan 2011년 3월 31일
No rows should have numbers???
joey
joey 2011년 3월 31일
no row shoul not have identical combination of numbers

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

채택된 답변

Teja Muppirala
Teja Muppirala 2011년 3월 31일
This is a simple (but inefficent) way to do it using UNIQUE:
Y = [];
while size(Y,1) ~= 3
Y = unique([Y ; randsample(10,3)'],'rows');
end;
Y = Y(randperm(size(Y,1)),:) %<-- To remove the sorting done by UNIQUE

추가 답변 (1개)

Titus Edelhofer
Titus Edelhofer 2011년 3월 31일
Hi Joey, not really elegant, but could work (as long as your "real" need is not much larger):
Y = zeros(3, 10);
for i=1:3
Y(i,:) = randperm(10);
end
Y = Y(:, 1:3);
Titus
  댓글 수: 3
Jan
Jan 2011년 3월 31일
Does your example "Y=[1 3 5;6 8 6;1 5 3]" have "the same combination of numbers" also? Please explain all of the wanted constraints exactly - it is impossible to guess, what you need.
joey
joey 2011년 3월 31일
Y=[1 3 4;1 6 9;6 7 8] ok
Y=[1 3 5;6 8 6;1 5 3] not okay
These are examples of 'ok' and 'not okay'
Each row should have number without repetition (I can do it with randsample(10,3))
Also Each row shoul not have identical combination of numbers

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

카테고리

Help CenterFile Exchange에서 Random Number Generation에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by