Here is one solution that will achieve the same result as your for loop (i.e. another column permutation 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 many ways to solve this problem. Here's another solution that avoids changing the matrix using linear indexing in A :
%# Solution 2: [r,c,p] = size(A); Aperm = zeros([rcp]); index1 = repmat(1:r,1,c*p); [~,index2] = sort(rand(c,p)); %# A variation on part of Amro answer index2 = kron(index2(:),ones(r,1)).'; %' index3 = kron(1:p,ones(1,r*c)); index = sub2ind([rcp],index1,index2,index3); Aperm(:) = A(index);
source share