我想生成包含用户定义范围中的数字的随机向量。例如: 数字= [1 2 4 10] 随机向量应始终由这些数字组成。例如。 rand_num = [1 1 2 10 5 2 10] 感谢。
答案 0 :(得分:2)
让
numbers = [1 2 4 10]; %// population to sample from
N = 5; %// how many samples to take
您可以使用randsample
(统计工具箱):
rand_num = randsample(numbers, N, true); %// "true" means sample with replacement
或randi
(标准函数):
rand_num = numbers(randi(numel(numbers), 1, N));