如何生成一系列随机对而不重复对? 以下代码已生成对,但不避免重复:
for k=1:8 Comb=[randi([-15,15]) ; randi([-15,15])]; T{1,k}=Comb; end跑步时我得到了:
T= [-3;10] [5;2] [1;-5] [10;9] [-4;-9] [-5;-9] [3;1] [-3;10]重复对[-3,10],这是不可能发生的。
PS:条目可以是正面的也可以是负面的。
这有什么内置功能吗? 任何解决这个问题的消化方法?
How to generate a sequence of random pairs without repeating pairs? The following code already generates the pairs, but does not avoid repetitions:
for k=1:8 Comb=[randi([-15,15]) ; randi([-15,15])]; T{1,k}=Comb; endWhen running I got:
T= [-3;10] [5;2] [1;-5] [10;9] [-4;-9] [-5;-9] [3;1] [-3;10]The pair [-3,10] is repeated, which cannot happen.
PS : The entries can be positive or negative.
Is there any built in function for this? Any sugestion to solve this?
最满意答案
如果您有统计工具箱,则可以使用randsample从1到31^2 (其中31是总体大小)中抽取8数字,而无需替换,然后将每个获得的数字“解包”到一对的两个组件中:
s = -15:15; % population M = 8; % desired number of samples N = numel(s); % population size y = randsample(N^2, M); % sample without replacement result = s([ceil(y/N) mod(y-1, N)+1]); % unpack pair and index into population示例运行:
result = 14 1 -5 7 13 -8 15 4 -6 -7 -6 15 2 3 9 6If you have the Statistics Toolbox, you can use randsample to sample 8 numbers from 1 to 31^2 (where 31 is the population size), without replacement, and then "unpack" each obtained number into the two components of a pair:
s = -15:15; % population M = 8; % desired number of samples N = numel(s); % population size y = randsample(N^2, M); % sample without replacement result = s([ceil(y/N) mod(y-1, N)+1]); % unpack pair and index into populationExample run:
result = 14 1 -5 7 13 -8 15 4 -6 -7 -6 15 2 3 9 6使用随机对生成单元格而不重复(Generate cell with random pairs without repetitions)如何生成一系列随机对而不重复对? 以下代码已生成对,但不避免重复:
for k=1:8 Comb=[randi([-15,15]) ; randi([-15,15])]; T{1,k}=Comb; end跑步时我得到了:
T= [-3;10] [5;2] [1;-5] [10;9] [-4;-9] [-5;-9] [3;1] [-3;10]重复对[-3,10],这是不可能发生的。
PS:条目可以是正面的也可以是负面的。
这有什么内置功能吗? 任何解决这个问题的消化方法?
How to generate a sequence of random pairs without repeating pairs? The following code already generates the pairs, but does not avoid repetitions:
for k=1:8 Comb=[randi([-15,15]) ; randi([-15,15])]; T{1,k}=Comb; endWhen running I got:
T= [-3;10] [5;2] [1;-5] [10;9] [-4;-9] [-5;-9] [3;1] [-3;10]The pair [-3,10] is repeated, which cannot happen.
PS : The entries can be positive or negative.
Is there any built in function for this? Any sugestion to solve this?
最满意答案
如果您有统计工具箱,则可以使用randsample从1到31^2 (其中31是总体大小)中抽取8数字,而无需替换,然后将每个获得的数字“解包”到一对的两个组件中:
s = -15:15; % population M = 8; % desired number of samples N = numel(s); % population size y = randsample(N^2, M); % sample without replacement result = s([ceil(y/N) mod(y-1, N)+1]); % unpack pair and index into population示例运行:
result = 14 1 -5 7 13 -8 15 4 -6 -7 -6 15 2 3 9 6If you have the Statistics Toolbox, you can use randsample to sample 8 numbers from 1 to 31^2 (where 31 is the population size), without replacement, and then "unpack" each obtained number into the two components of a pair:
s = -15:15; % population M = 8; % desired number of samples N = numel(s); % population size y = randsample(N^2, M); % sample without replacement result = s([ceil(y/N) mod(y-1, N)+1]); % unpack pair and index into populationExample run:
result = 14 1 -5 7 13 -8 15 4 -6 -7 -6 15 2 3 9 6
发布评论