How to generate random vector from specific user d

2019-08-18 07:12发布

问题:

I want to generate the random vector containing the number from the user defined range. For example: numbers = [1 2 4 10] The random vector should always consist of these numbers. Eg. rand_num= [1 1 2 10 5 2 10] Thanks.

回答1:

Let

numbers = [1 2 4 10]; %// population to sample from
N = 5; %// how many samples to take

You can use randsample (Statistics Toolbox):

rand_num = randsample(numbers, N, true); %// "true" means sample with replacement

or randi (standard function):

rand_num = numbers(randi(numel(numbers), 1, N));