annotate general/arrutils/permutegroups.m @ 61:eff6bddf82e3 tip

Finally implemented perceptual brightness thing.
author samer
date Sun, 11 Oct 2015 10:20:42 +0100
parents e44f49929e56
children
rev   line source
samer@4 1 function A=permutegroups(A,doms,perm)
samer@4 2 % permutegroups - Reorder dimensions of array in groups
samer@4 3 %
samer@4 4 % Like PERMUTE, PERMUTEGROUPS rearranges the dimensions of a
samer@4 5 % multidimensional array in the same way that transpose swaps
samer@4 6 % the 2 dimensions of a matrix. However, rather than specifying
samer@4 7 % the permutation in terms of individual dimensions, the
samer@4 8 % dimensions are matched to a groups template and the then
samer@4 9 % the groups are permuted. Eg, suppose A is an array such that
samer@4 10 % the index domain size(A)=[6 3 4 7]. Now suppose the these
samer@4 11 % are grouped as [ [6 3] [4 7] ] and we wish to swap the groups.
samer@4 12 % Rather than saying
samer@4 13 %
samer@4 14 % permute(A,[3 4 1 2])
samer@4 15 %
samer@4 16 % we can say
samer@4 17 %
samer@4 18 % permutegroups(A,{[6 3] [4 7]},[2 1])
samer@4 19 %
samer@4 20 % where the 2nd parameter specifies the two groups, and the third
samer@4 21 % indicates that we want to swap them. Actually, the numbers in the
samer@4 22 % group specification are not important - only the lengths of the
samer@4 23 % groups matters.
samer@4 24 %
samer@4 25 % Usage: B=permutegroups(A,groups,perm)
samer@4 26 %
samer@4 27 % A: orginal array
samer@4 28 % groups: list of array subdomains (ie sub-sequences of size(A))
samer@4 29 % One of these may be nan, in which case, all remaining dims
samer@4 30 % are assigned to that group.
samer@4 31 % perm: desired permutation of groups
samer@4 32
samer@4 33 a=0;
samer@4 34 dims=cell(size(doms));
samer@4 35 nanat=0;
samer@4 36 for i=1:length(doms)
samer@4 37 if isempty(doms{i}), dims{i}=[];
samer@4 38 else
samer@4 39 if ~isnan(doms{i})
samer@4 40 l=length(doms{i});
samer@4 41 dims{i}=a+(1:l);
samer@4 42 a=a+l;
samer@4 43 else
samer@4 44 nanat=i;
samer@4 45 dims{i}=a;
samer@4 46 end
samer@4 47 end
samer@4 48 end
samer@4 49
samer@4 50 if nanat>0
samer@4 51 gap=length(size1(A))-a;
samer@4 52 if gap>0,
samer@4 53 dims{nanat}=dims{nanat}+(1:gap);
samer@4 54 for j=nanat+1:length(doms)
samer@4 55 if ~isempty(dims{j}), dims{j}=dims{j}+gap; end
samer@4 56 end
samer@4 57 else
samer@4 58 dims{nanat}=[];
samer@4 59 end
samer@4 60 else
samer@4 61 gap=0;
samer@4 62 end
samer@4 63
samer@4 64 perm=cell2mat(dims(perm));
samer@4 65 maxdim=a+gap;
samer@4 66 if maxdim<1, perm(end+1)=1; end
samer@4 67 if maxdim<2, perm(end+1)=2; end
samer@4 68
samer@4 69 A=permute(A,perm);