0

I am trying to make 2 different sample arrays, I know how to make a sample like this:

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

But I would like x_2 to be a different sample, I mean that not any element of the array be repeated in the other x_1 array. Is there any easy function in Matlab to do this without having to test each element and change it manually?

Thanks for the help

1 Answer 1

3

The easiest way to do this is to just sample twice as many times from the distribution and then split the result into two pieces. Then you are guaranteed to have no repeated values within or between the arrays.

% 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

An alternative approach if you want to allow duplicates within an array. Then you'd just modify the input sample to the second randsample call by removing anything that appeared in the first.

% 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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.