samer@4: function A=permutegroups(A,doms,perm) samer@4: % permutegroups - Reorder dimensions of array in groups samer@4: % samer@4: % Like PERMUTE, PERMUTEGROUPS rearranges the dimensions of a samer@4: % multidimensional array in the same way that transpose swaps samer@4: % the 2 dimensions of a matrix. However, rather than specifying samer@4: % the permutation in terms of individual dimensions, the samer@4: % dimensions are matched to a groups template and the then samer@4: % the groups are permuted. Eg, suppose A is an array such that samer@4: % the index domain size(A)=[6 3 4 7]. Now suppose the these samer@4: % are grouped as [ [6 3] [4 7] ] and we wish to swap the groups. samer@4: % Rather than saying samer@4: % samer@4: % permute(A,[3 4 1 2]) samer@4: % samer@4: % we can say samer@4: % samer@4: % permutegroups(A,{[6 3] [4 7]},[2 1]) samer@4: % samer@4: % where the 2nd parameter specifies the two groups, and the third samer@4: % indicates that we want to swap them. Actually, the numbers in the samer@4: % group specification are not important - only the lengths of the samer@4: % groups matters. samer@4: % samer@4: % Usage: B=permutegroups(A,groups,perm) samer@4: % samer@4: % A: orginal array samer@4: % groups: list of array subdomains (ie sub-sequences of size(A)) samer@4: % One of these may be nan, in which case, all remaining dims samer@4: % are assigned to that group. samer@4: % perm: desired permutation of groups samer@4: samer@4: a=0; samer@4: dims=cell(size(doms)); samer@4: nanat=0; samer@4: for i=1:length(doms) samer@4: if isempty(doms{i}), dims{i}=[]; samer@4: else samer@4: if ~isnan(doms{i}) samer@4: l=length(doms{i}); samer@4: dims{i}=a+(1:l); samer@4: a=a+l; samer@4: else samer@4: nanat=i; samer@4: dims{i}=a; samer@4: end samer@4: end samer@4: end samer@4: samer@4: if nanat>0 samer@4: gap=length(size1(A))-a; samer@4: if gap>0, samer@4: dims{nanat}=dims{nanat}+(1:gap); samer@4: for j=nanat+1:length(doms) samer@4: if ~isempty(dims{j}), dims{j}=dims{j}+gap; end samer@4: end samer@4: else samer@4: dims{nanat}=[]; samer@4: end samer@4: else samer@4: gap=0; samer@4: end samer@4: samer@4: perm=cell2mat(dims(perm)); samer@4: maxdim=a+gap; samer@4: if maxdim<1, perm(end+1)=1; end samer@4: if maxdim<2, perm(end+1)=2; end samer@4: samer@4: A=permute(A,perm);