如何在MATLAB中随机置换3D矩阵中的列 [英] How to randomly permute columns in 3D matrix in MATLAB

查看:124
本文介绍了如何在MATLAB中随机置换3D矩阵中的列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have 3D matrix (10000 x 60 x 20) and I need to permute 2nd and 3rd dimensions keeping the columns intact.

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

For 2D matrix I use RANDPERM:

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

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

I cannot just apply RANDPERM twice - column index first, then page index. Not enough randomization.

一种解决方案是将矩阵从3D变形为2D,将列和页面压缩为列,对它们进行置换,然后重新成形.但是我也想以这样的方式进行排列,即每个页面的列都独立排列.像这样:

One solution is to reshape the matrix from 3D to 2D squeezing columns and pages to columns, permute them and then reshape back. But I'd also like to do permutation in such a way that columns permuted independently for each page. Something like:

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

我可以更有效地做到吗?还有更好的方法吗?

Can I do it more efficiently? Any better ways?

推荐答案

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

将矩阵从3D重塑为2D 挤压列和页面 列,置换它们,然后重塑 后退

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循环)


Solution#2: Columns are permuted within each pages independently (equivalent to your for-loop)

我也想在 列排列的方式 每个页面独立

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中随机置换3D矩阵中的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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