Matlab:范围内无重复的随机整数 [英] Matlab: random integers from a range with no repetition

查看:77
本文介绍了Matlab:范围内无重复的随机整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用不重复的范围内包含随机整数的数组,我用过:

I want to get an array with randomized integer included in range with no repeat, I used :

randperm(15,3)

输出为:

8, 10, 12

此函数不使用范围,我想仅从10个随机值中选择一个.

This function not use a range, I would like to random values only from 10 e.g.

推荐答案

如果拥有统计信息"工具箱,则可以使用 randsample ,无需替换.

If you have the Statistics toolbox, you can use randsample without replacement.

% MATLAB R2017a
LB = 3;   % lower bound of range (integer)
UB = 17;  % upper bound of range (integer),  UB > LB
randsample(LB:UB,3,'false') 

@ Wolfie 的方法使用


请注意, randi 可行可以很好地处理范围内的均匀分布(离散均匀)整数,但可以对进行替换采样(可以重复).据我所知,这不需要任何工具箱,但需要结合一个过程来删除重复项并重新采样,直到所有整数都是唯一的为止.


Notice that randi works well for uniformly distributed (discrete uniform) integers in a range but it samples with replacement (can give duplicates). This requires no toolbox to my knowledge but would require combining with a procedure to remove duplicates and resample until all integers were unique.

randi([LB UB],3,1)

注意:如果@Wolfie将其发布为答案或可以使此答案成为社区Wiki,则将删除@ Wolfie 的方法.基于共识.

Note: will remove @Wolfie's method if @Wolfie posts it as answer or can make this answer community wiki based on consensus.

这篇关于Matlab:范围内无重复的随机整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆