How to generate random vector from specific user d

2019-08-18 07:15发布

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条回答
甜甜的少女心
2楼-- · 2019-08-18 08:00

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));
查看更多
登录 后发表回答