如何从特定的用户定义范围生成随机向量? [英] How to generate random vector from specific user defined range?
本文介绍了如何从特定的用户定义范围生成随机向量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想生成包含用户定义范围内的数字的随机向量.例如: 数字= [1 2 4 10] 随机向量应始终由这些数字组成.例如. rand_num = [1 1 2 10 5 2 10] 谢谢.
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.
推荐答案
让我们
numbers = [1 2 4 10]; %// population to sample from
N = 5; %// how many samples to take
您可以使用 randsample
(统计工具箱):
You can use randsample
(Statistics Toolbox):
rand_num = randsample(numbers, N, true); %// "true" means sample with replacement
或 randi
(标准功能):
or randi
(standard function):
rand_num = numbers(randi(numel(numbers), 1, N));
这篇关于如何从特定的用户定义范围生成随机向量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文