How to generate random vector from specific user defined range?

馋奶兔 提交于 2020-01-07 05:56:11

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!