I have 3D matrix (10000 x 60 x 20) and I need to permute 2nd and 3rd dimensions keeping the columns intact.
For 2D matrix I use RANDPERM:
pidx = randperm(size(A,2));
Aperm = A(:,pidx);
I cannot just apply RANDPERM twice - column index first, then page index. Not enough randomization.
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?
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]);
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]);
Here's one solution that will achieve the same result as your for loop (i.e. a different permutation of columns for each page):
%# Solution 1:
[r,c,p] = size(A);
Aperm = reshape(A,r,c*p);
index = arrayfun(@randperm,c.*ones(1,p),'UniformOutput',false);
index = [index{:}]+kron(0:c:c*(p-1),ones(1,c));
Aperm = reshape(Aperm(:,index),r,c,p);
And, as with many problems in MATLAB, there are a number of different ways to solve this. Here's another solution that avoids reshaping of the matrix by using only linear indexing into A
:
%# Solution 2:
[r,c,p] = size(A);
Aperm = zeros([r c p]);
index1 = repmat(1:r,1,c*p);
[~,index2] = sort(rand(c,p)); %# A variation on part of Amro's answer
index2 = kron(index2(:),ones(r,1)).'; %'
index3 = kron(1:p,ones(1,r*c));
index = sub2ind([r c p],index1,index2,index3);
Aperm(:) = A(index);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With