是否有随机洗牌在MATLAB矩阵更好/更快的方法? [英] Is there a better/faster way of randomly shuffling a matrix in MATLAB?
问题描述
在MATLAB中,我使用shake.m功能(<一个href=\"http://www.mathworks.com/matlabcentral/fileexchange/10067-shake\">http://www.mathworks.com/matlabcentral/fileexchange/10067-shake)随机洗牌每一列,例如:
In MATLAB, I am using the shake.m function (http://www.mathworks.com/matlabcentral/fileexchange/10067-shake) to randomly shuffle each column. For example:
a = [1 2 3; 4 5 6; 7 8 9]
a =
1 2 3
4 5 6
7 8 9
b = shake(a)
b =
7 8 6
1 5 9
4 2 3
此功能不正是我想要的,但我列的非常的长(> 10,000,000),因此这需要很长时间才能运行。有谁知道实现这一目标的一个更快的方法?我试图分别摇动每个列向量但这不是更快。谢谢!
This function does exactly what I want, however my columns are very long (>10,000,000) and so this takes a long time to run. Does anyone know of a faster way of achieving this? I have tried shaking each column vector separately but this isn't faster. Thanks!
推荐答案
下面是一个简单的量化方法。请注意,它创建了一个辅助的基质( IND
)的大小相同 A
,所以这取决于你的记忆可能可用与否。
Here's a simple vectorized approach. Note that it creates an auxiliary matrix (ind
) the same size as a
, so depending on your memory it may be usable or not.
[~, ind] = sort(rand(size(a))); %// create a random sorting for each column
b = a(bsxfun(@plus, ind, 0:size(a,1):numel(a)-1)); %// convert to linear index
这篇关于是否有随机洗牌在MATLAB矩阵更好/更快的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!