Random numbers without repetition
9 次查看(过去 30 天)
显示 更早的评论
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!
采纳的回答
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
0 个评论
更多回答(1 个)
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
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Random Number Generation 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!