function P1 = reorderP(P,m,n,pi) % pi is a row vector permutation dims = repmat([m],[1 n]); P1 = 0*P; for xi = 1:m^n x = ind2coord(xi,dims); y = applyperm(pi,x); yi = coord2ind(y,dims); P1(yi) = P(xi); end return function y = applyperm(pi,x) y = x; for i=1:length(x) y(i) = x(pi(i)); end return