Home > GLMdenoise > utilities > permutedim.m

permutedim

PURPOSE ^

function [f,perm] = permutedim(m,dim,perm,wantind)

SYNOPSIS ^

function [f,perm] = permutedim(m,dim,perm,wantind)

DESCRIPTION ^

 function [f,perm] = permutedim(m,dim,perm,wantind)

 <m> is a matrix
 <dim> (optional) is a dimension of <m>.  special case is 0 which means permute globally.
   another special case is -1 which means permute globally and ensure that the
   original order is NOT obtained (however, if <perm> is supplied, that takes
   precedence).  default: 0.
 <perm> (optional) is the permutation order to use
 <wantind> (optional) is whether you want individual cases
   permuted randomly.  default: 0.  note that when <dim> is 0 or -1,
   <wantind> has no meaning.

 randomly shuffle <m> along <dim> or globally.
 also return <perm>, the permutation order used.

 example:
 a = repmat(1:9,[5 1])
 b = permutedim(a,2)
 b2 = permutedim(a,2,[],1)
 b3 = permutedim(a,0)

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [f,perm] = permutedim(m,dim,perm,wantind)
0002 
0003 % function [f,perm] = permutedim(m,dim,perm,wantind)
0004 %
0005 % <m> is a matrix
0006 % <dim> (optional) is a dimension of <m>.  special case is 0 which means permute globally.
0007 %   another special case is -1 which means permute globally and ensure that the
0008 %   original order is NOT obtained (however, if <perm> is supplied, that takes
0009 %   precedence).  default: 0.
0010 % <perm> (optional) is the permutation order to use
0011 % <wantind> (optional) is whether you want individual cases
0012 %   permuted randomly.  default: 0.  note that when <dim> is 0 or -1,
0013 %   <wantind> has no meaning.
0014 %
0015 % randomly shuffle <m> along <dim> or globally.
0016 % also return <perm>, the permutation order used.
0017 %
0018 % example:
0019 % a = repmat(1:9,[5 1])
0020 % b = permutedim(a,2)
0021 % b2 = permutedim(a,2,[],1)
0022 % b3 = permutedim(a,0)
0023 
0024 % deal with input
0025 if ~exist('dim','var') || isempty(dim)
0026   dim = 0;
0027 end
0028 if ~exist('perm','var') || isempty(perm)
0029   perm = [];
0030 end
0031 if ~exist('wantind','var') || isempty(wantind)
0032   wantind = 0;
0033 end
0034 
0035 % do it
0036 if dim==0 || dim==-1
0037 
0038   % figure out perm
0039   if isempty(perm)
0040     while 1
0041       perm = randperm(length(m(:)));
0042       if dim==-1
0043         if ~isequal(perm,1:length(m(:)))
0044           break;
0045         end
0046       else
0047         break;
0048       end
0049     end
0050   end
0051   
0052   % do it
0053   f = reshape(m(perm),size(m));
0054 
0055 else
0056   if wantind
0057   
0058     % make 2D
0059     f = reshape2D(m,dim);
0060     
0061     % figure out perm
0062     if isempty(perm)
0063       [d,perm] = sort(rand(size(f,1),size(f,2)));  % figure out random permutation for each column
0064       perm = perm + repmat((0:size(f,2)-1)*size(f,1),[size(f,1) 1]);  % add appropriate offsets
0065     end
0066     
0067     % index into f and then undo the 2D
0068     f = reshape2D_undo(f(perm),dim,size(m));
0069   
0070   else
0071   
0072     % figure out perm
0073     if isempty(perm)
0074       perm = randperm(size(m,dim));
0075     end
0076   
0077     % construct indices
0078     indices = repmat({':'},[1 max(ndims(m),dim)]);
0079     indices{dim} = perm;
0080     
0081     % do it
0082     f = subscript(m,indices);
0083   
0084   end
0085 end

Generated on Fri 01-Aug-2014 12:03:17 by m2html © 2005