如何在MATLAB中随机置换三维矩阵中的列

yuk*_*yuk 6 random matlab permutation matrix

我有3D矩阵(10000 x 60 x 20),我需要置换第二维和第三维,保持列完好无损.

对于2D矩阵,我使用RANDPERM:

pidx = randperm(size(A,2));
Aperm = A(:,pidx);
Run Code Online (Sandbox Code Playgroud)

我不能只应用RANDPERM两次 - 首先是列索引,然后是页面索引.没有足够的随机化.

一种解决方案是将矩阵从3D压缩到2D,将页面和页面压缩到列,对其进行置换,然后重新整形.但我也想以这样一种方式进行排列,即每列独立地排列列.就像是:

Aperm = zeros(size(A));
for p=1:size(A,3)
    pidx = randperm(size(A,2));
    Aperm(:,:,p) = A(:,pidx,p);
end
Run Code Online (Sandbox Code Playgroud)

我能更有效地完成吗?有更好的方法吗?

Amr*_*mro 3

解决方案#1:在所有页面上排列列

将矩阵从 3D 重塑为 2D,将列和页面压缩为列,排列它们,然后重塑回来

A = randi(10, [3 4 2]);                 %# some random 3D matrix

[r c p] = size(A);
Aperm = reshape(A, [r c*p]);
Aperm = reshape(Aperm(:,randperm(c*p)), [r c p]);
Run Code Online (Sandbox Code Playgroud)

解决方案#2:列在每个页面内独立排列(相当于 for 循环)

我还想以这样的方式进行排列,即每个页面的列独立排列

A = randi(10, [3 4 2]);                 %# some random 3D matrix

[r c p] = size(A);
Aperm = reshape(A, [r c*p]);

[~,idx] = sort(rand(p,c),2);            %# this is what RANDPERM does
idx = reshape(bsxfun(@plus, idx',0:c:c*(p-1)),1,[]);    %'#

Aperm = reshape(Aperm(:,idx), [r c p]);
Run Code Online (Sandbox Code Playgroud)