在MATLAB上从0-9创建随机数字序列 [英] Create random sequences of numbers from 0-9 on MATLAB

查看:554
本文介绍了在MATLAB上从0-9创建随机数字序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建几个包含从0到9的所有数字的随机序列.该序列必须是随机的且无重复,并且需要包含0和9.我需要使用MATLAB进行此操作.

I need to create several random sequences containing all digits from 0 through 9. The sequence needs to be random with no repetition and needs to include 0 and 9. I need to do this using MATLAB.

请帮助!

推荐答案

使用 randperm 为您执行此操作.您指定一个输入数字N(例如您的示例中的10),它返回一个大小为N的向量,该向量从1N随机放置,从而执行选择而不进行替换(也称为置换) ).因此,请在MATLAB中执行以下操作:

Use randperm to do this for you. You specify an input number N (like 10 in your example), and it returns a vector of size N of randomly placed elements from 1 to N, such that it performs selection without replacement (a.k.a. a permutation). As such, do this in MATLAB:

rng(123); %// Set seed for reproducibility - 
          %// Not required... doing it so you can repeat my results
out = randperm(10) - 1;

这就是我得到的:

out =

 2     1     9     5     8     3     7     0     4     6

请注意,我必须从输出中减去 1.原因是因为randperm会生成一个从1N的向量,而您想要的值是从0N-1.

Take note that I had to subtract 1 from the output. The reason why is because randperm generates a vector from 1 to N, while you want values going from 0 to N-1.

如果要创建几个随机序列,请创建一个矩阵,其中行数与所需序列数一样长,列数与所需数一样长您只需要一个序列(本例中为10).之后,运行for循环以生成每个序列,并将其存储到矩阵中的单独行中.

If you want to create several random sequences, create a matrix where the number of rows is as long as the amount of sequences you want, and the number of columns is as long as how many numbers you want in a single sequence (10 in our case). After, run a for loop to generate each sequence and store this into a separate row in the matrix.

您还可以使用 arrayfun 来实现此目的,然后使用 cell2mat 转换结果.这是必需的,因为每次运行randpermarrayfun都会输出一个cell结果数组,因此我们需要将所有这些单个单元格数组都转换回单个矩阵.同样,这些序列将位于单行列的单元格数组中,因此在使用cell2mat进行转换之前,我们需要对其进行转置.换句话说,请尝试执行以下操作:

You could also achieve this using arrayfun, then convert the results back using cell2mat. This is required because arrayfun will output a cell array of results each time we run randperm, so we need to convert all of these individual cell arrays back into a single matrix. Also, these sequences will be in a cell array of a single row of columns, and so we need to transpose this prior to converting using cell2mat. In other words, try doing this:

rng(123);
numSequences = 5; %// Choose how many sequences you want here
out = arrayfun(@(x) randperm(10) - 1, 1:numSequences, 'uni', 0);
out = cell2mat(out.');

我因此得到:

out = 

 2     1     9     5     8     3     7     0     4     6
 3     7     6     0     4     2     8     9     1     5
 7     8     5     6     3     9     0     4     2     1
 0     5     4     6     2     1     3     9     7     8
 1     5     2     3     6     8     9     0     4     7


编辑

您想从0-9生成10个随机数,但只能从此随机生成的集合中选择一个子集(如5).如果您查看randperm,则可以指定第二个参数,在该参数处告诉它最终要选择多少个数字.如果省略此参数,则默认为您指定的第一个参数N.这样,只需更改代码以包含此附加参数即可.因此:


Edit

You would like generate 10 random numbers from 0 - 9, but only select a subset (like 5) from this randomly generated set. If you look at randperm, there is a second parameter you specify where you tell it how many numbers you ultimately want to select. If you omit this parameter, it defaults to the first parameter you specified N. As such, simply change the code to include this additional parameter. As such:

rng(123);
numSequences = 5; %// Choose how many sequences you want here
maxNumber = 10; %// Specify how many numbers to possibly generate from
subsetNumber = 5; %// How many numbers you want to select from the list of numbers
out = arrayfun(@(x) randperm(maxNumber, subsetNumber) - 1, 1:numSequences, 'uni', 0);
out = cell2mat(out.');

上面说我想从[0,9]生成数字,但是只能从此列表中选择5个数字.这样,我得到以下输出:

The above says that I want to generate numbers from [0,9], but only choose 5 numbers from this list. As such, I get the following output:

out =

 1     9     8     2     5
 4     0     7     1     3
 6     3     1     7     5
 3     0     9     5     1
 5     1     2     8     6


for循环方法

如果您是MATLAB的初学者,则可以按照我之前所述的方法for循环进行. 乔恩·斯基特(Jon Skeet),他在其中一篇文章中的StackOverflow忍者


for loop approach

If you're more of a beginner to MATLAB, you could do this the for loop way as I stated before. Jon Skeet, the StackOverflow ninja in one of his posts recommends that you write code for readability first, then optimize after (if you can). I agree with this, and the for loop approach is definitely more readable. It is essentially producing what arrayfun combined with cellmat are doing. As such, you would do it the for loop way like so:

rng(123);
numSequences = 5; %// Choose how many sequences you want here
maxNumber = 10; %// Specify how many numbers to possibly generate from
subsetNumber = 5; %// How many numbers you want to select from the list of numbers
out = zeros(numSequences, subsetNumber); %// Pre-allocate a numSequences x 10 matrix of zeroes
for idx = 1 : numSequences
    out(idx,:) = randperm(maxNumber, subsetNumber) - 1;
end

您将获得与上面向您展示的第一种方法相同的结果,因此在此不再赘述.

You will get the same results as doing it the first approach that I showed you above, so I won't repeat them here.

请注意,每行有一个随机序列.要访问第i 序列,只需执行以下操作:

Take note that we have a single random sequence per row. To access the ith sequence, simply do:

out(idx,:)

idx将是您要访问的行.这将返回与您想要的行相对应的随机排列.

idx would be the row you want to access. This will return a random permutation corresponding to that row you want.

这篇关于在MATLAB上从0-9创建随机数字序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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