2016-06-21 33 views
0

我试图让2个不同样本数组,我知道如何使这样一个样本:使得2个不同样本数组(不重复的数组元素)

x_1 = randsample(1:10,5,true); 
x_2 = randsample(1:10,5,true); 

但我想x_2是一个不同的样本,我的意思是没有任何元素的阵列在其他x_1阵列重复。在Matlab中有没有简单的功能来做到这一点,而无需测试每个元素并手动更改它?

感谢您的帮助

回答

3

要做到这一点,只是从分发样本的两倍时间,那么最简单的方法拆分结果分为两个部分。然后保证在阵列内或阵列之间没有重复的值。

% Sample 10 times instead of 5 (5 for each output) 
samples = randsample(1:10, 10); 

%  2 10  4  5  3  8  7  1  6  9 

% Put half of these samples in x1 
x1 = samples(1:5); 

%  2 10  4  5  3 


% And the other half in x2 
x2 = samples(6:end); 

%  8  7  1  6  9 

,如果你想允许重复内阵列另一种方法。然后,您只需将输入样本修改为第二个randsample呼叫,方法是删除第一个中显示的任何内容。

% Define the initial pool of samples to draw from 
samples = 1:10; 

x1 = randsample(samples, 5, true); 

%  5  4  8  8  2 

% Remove things in x1 from samples 
samples = samples(~ismember(samples, x1)); 

x2 = randsample(samples, 5, true); 

%  6  6  7  9  9