2016-06-21 38 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