在MATLAB中生成随机数

时间:2014-05-30 20:08:20

标签: matlab random

我想生成三组随机变量,我不希望它们是相同的

例如,如果我想生成以下集合,范围从1到10

Set1= [ 1 4 ] 
set2= [ 3 5 ]
Set3= [ 7 9]

3 个答案:

答案 0 :(得分:0)

您可以使用“randperm”函数创建一个唯一随机数组,然后将数组分成几组。

x = randperm(10,6);
Set1 = x(1:2);
Set2 = x(3:4);
Set3 = x(5:6);

答案 1 :(得分:0)

在为每个集合选择两个元素之后,必须随机化剩余的元素以供下一次迭代选择。下面的代码试图实现它 -

set123 = zeros(3,2); %// Pre-allocate for the output
array1 = 1:10; %// Array of numbers to choose from for the first iteration
for k = 1:3
    %// Create many possible combinations of such two numbers from the sets.
    %// The sets would have 10 elements to choose from at the start and
    %// then 8 and then 6.
    combs = nchoosek(array1,2);

    %// At each stage all the possible combinations of the pairs must be
    %// juggled and one of them must be selected randomly and then indexed
    %// to get the set for this iteration
    choosen_rowind = randperm(size(combs,1),1);
    set123(k,:) = combs(choosen_rowind,:);

    %// Setup the array of numbers left to choose from the next iteration
    array1 = setdiff(array1,set123);    
end
%// set123 is the desired output in a single array

答案 2 :(得分:0)

如果我理解正确,您希望从{{{1}集合中r - 次 - c个随机变量(在您的情况下为r=3c=2) }},m,...,m+1}(在您的情况下为nn=1),不重复。我假设你想要(联合)统一分布。

为此您使用randsample

m=10
相关问题