MATLAB爱好者论坛-LabFans.com

MATLAB爱好者论坛-LabFans.com (https://www.labfans.com/bbs/index.php)
-   MATLAB论坛 (https://www.labfans.com/bbs/forumdisplay.php?f=6)
-   -   [MATLAB基础] 随机 (https://www.labfans.com/bbs/showthread.php?t=8689)

drosophilia 2009-05-28 16:08

随机
 
1到10之间的自然数随机调3个 怎么做

或者是这么一个问题

随机挑几个数
一个大向量
[1 2 3 5 7 9 8 11 43 21]
想随机从中挑三个?怎么挑

zsy312 2009-05-28 21:11

回复: 随机
 
用rand,但指标有可能重复,所以有点缺陷:
x=[1 2 3 5 7 9 8 11 43 21];
N=3;%挑选的项数
a=ceil(10*rand(1,N));%生成随机数组,指标可能重复
y=x(a);

slgu 2009-05-28 22:46

回复: 随机
 
x=[1 2 3 5 7 9 8 11 43 21];
x(randperm(10))

drosophilia 2009-05-28 23:50

回复: 随机
 
x=[1 2 3 5 7 9 8 11 43 21];
y=randperm(10)
z=y(1:3)
x(z)

TTT_IOU 2009-05-29 22:44

回复: 随机
 
a为一大向量;
b=length(a);
c=randperm(b);
d=a(c(1:3));
d


所有时间均为北京时间。现在的时间是 11:04

Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.