问题
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));
来源:https://stackoverflow.com/questions/24620729/how-to-generate-random-vector-from-specific-user-defined-range