randperm()可以生成几个随机排列吗? [英] Can randperm() generate several random permutations?

查看:187
本文介绍了randperm()可以生成几个随机排列吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Matlab中

p = randperm(n,k)返回一个行向量,其中包含k个唯一的整数,这些整数从1到n(含1和n)之间随机选择.

p = randperm(n,k) returns a row vector containing k unique integers selected randomly from 1 to n inclusive.

对randperm()的一次调用能否返回几行向量,每行如上所述? 如果没有,还有其他方法可以生成几个随机排列吗?

Can one call to randperm() return several rows of vectors, each of which is as above? If not, is there other way to generate several random permutations?

在这种情况下,避免循环会更快吗?

Will avoiding loop necessarily be faster in this case?

谢谢!

推荐答案

RANDPERM 本身返回只有一个排列.如果要避免循环,可以使用ARRAYFUN调用它:

RANDPERM itself returns only one permutation. If you want to avoid loop you can call it with ARRAYFUN:

Nperm = 5; 
N = 6;
result = arrayfun(@(x)randperm(N),(1:Nperm)','UniformOutput',0);

这将返回Nperm x 1个单元格数组.要将其转换为矩阵,可以使用CELL2MAT:

This will return Nperm x 1 cell array. To convert it to matrix you can use CELL2MAT:

result = cell2mat(result);

还有 PERMS 函数可返回所有排列,但仅适用于一小部分数字.

There is also PERMS function that returns all permutations, but it only practical for a small numbers.

还要检查FileExchange提交的内容 ALLCOMB 其他.

Check also FileExchange submissions ALLCOMB, PERMS and others.

这篇关于randperm()可以生成几个随机排列吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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