如何从特定用户定义的范围生成随机向量?

时间:2014-07-07 22:09:37

标签: matlab random octave random-sample

我想生成包含用户定义范围中的数字的随机向量。例如: 数字= [1 2 4 10] 随机向量应始终由这些数字组成。例如。 rand_num = [1 1 2 10 5 2 10] 感谢。

1 个答案:

答案 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));