gpt4 book ai didi

matlab - 如何在 MATLAB 中随机排列 3D 矩阵中的列

转载 作者:太空宇宙 更新时间:2023-11-03 19:34:31 27 4
gpt4 key购买 nike

我有 3D 矩阵 (10000 x 60 x 20),我需要置换第 2 维和第 3 维以保持列的完整性。

对于二维矩阵,我使用 RANDPERM:

pidx = randperm(size(A,2));
Aperm = A(:,pidx);

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

一种解决方案是将矩阵从 3D reshape 为 2D,将列和页面压缩为列,排列它们,然后 reshape 回来。但我也想以这样一种方式进行排列,即每一页的列独立排列。像这样的东西:

Aperm = zeros(size(A));
for p=1:size(A,3)
pidx = randperm(size(A,2));
Aperm(:,:,p) = A(:,pidx,p);
end

我可以更有效地做到这一点吗?有什么更好的方法吗?

最佳答案

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

reshape the matrix from 3D to 2D squeezing columns and pages to columns, permute them and then reshape back

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]);

解决方案#2:在每个页面内独立排列列(等同于您的 for 循环)

I'd also like to do permutation in such a way that columns permuted independently for each page

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]);

关于matlab - 如何在 MATLAB 中随机排列 3D 矩阵中的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6431048/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com