MATLAB以不同方式随机排列列 [英] MATLAB randomly permuting columns differently

查看:191
本文介绍了MATLAB以不同方式随机排列列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常大的矩阵A,具有N行和M列.我基本上想做以下操作

I have a very large matrix A with N rows and M columns. I want to basically do the following operation

for k = 1:N
    A(k,:) = A(k,randperm(M));
end

但快速高效. (M和N都很大,这只是一个更大的外循环中的一个内循环.)

but fast and efficiently. (Both M and N are very large, and this is only an inner loop in a more massive outer loop.)

更多背景信息:我正在尝试为相关矩阵实施置换测试( http: //en.wikipedia.org/wiki/Resampling_%28statistics%29 ).我的数据很大,我很不耐烦.如果有人知道实现这种测试的快速方法,我也将不胜感激,听听您的意见!

More context: I am trying to implement a permutation test for a correlation matrix (http://en.wikipedia.org/wiki/Resampling_%28statistics%29). My data is very large and I am very impatient. If anyone knows of a fast way to implement such a test, I would also be grateful to hear your input!

我是否希望避免循环执行此操作?

Do I have any hope of avoiding doing this in a loop?

很抱歉,如果已经问过这个问题.谢谢!

Apologies if this has already been asked. Thanks!

推荐答案

如果键入open randperm(至少在Matlab R2010b中),您会看到输入M的输出p只是

If you type open randperm (at least in Matlab R2010b) you'll see that its output p for an input M is just

[~, p] = sort(rand(1,M));

因此,要将其向量化为N行,

So, to vectorize this for N rows,

[~, P] = sort(rand(N,M), 2);

因此,生成P并使用线性索引进入A:

Thus, generate P and use linear indexing into A:

[~, P] = sort(rand(N,M), 2);
A = A(bsxfun(@plus, (1:N).', (P-1)*N));

示例:给定

N = 3;
M = 4;
A = [ 1     2     3     4
      5     6     7     8
      9    10    11    12 ];

一个(随机)结果是

A =
     2     3     1     4
     7     5     8     6
     9    11    12    10

这篇关于MATLAB以不同方式随机排列列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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