Home > helper > shift.m

shift

PURPOSE ^

--------------------------------------------------------------------------

SYNOPSIS ^

function newX = shift( x, dir, k)

DESCRIPTION ^

--------------------------------------------------------------------------
Shift matrix in any direction (up down left right) k elements.

 EXAMPLE
   x = linspace(1,10,10);
   y = shift( x, 'right', 3);

 INPUT
   x          - 1D or 2D  matrix
   dir        - shiting direction (eg. 'down', 'up', 'right', 'left')
   k          - number of elements shifted

 OUTPUT
   newX       - the shifted matrix

 SEE ALSO
   ndims, strcmp

 Written by Rich Nguyen (rich.uncc@gmail.com).
 Based on 'MATLAB array manipulation tips and tricks' by Peter Acklam.
 Version 1.0 June 2010
--------------------------------------------------------------------------

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function newX = shift( x, dir, k)
0002 %--------------------------------------------------------------------------
0003 %Shift matrix in any direction (up down left right) k elements.
0004 %
0005 % EXAMPLE
0006 %   x = linspace(1,10,10);
0007 %   y = shift( x, 'right', 3);
0008 %
0009 % INPUT
0010 %   x          - 1D or 2D  matrix
0011 %   dir        - shiting direction (eg. 'down', 'up', 'right', 'left')
0012 %   k          - number of elements shifted
0013 %
0014 % OUTPUT
0015 %   newX       - the shifted matrix
0016 %
0017 % SEE ALSO
0018 %   ndims, strcmp
0019 %
0020 % Written by Rich Nguyen (rich.uncc@gmail.com).
0021 % Based on 'MATLAB array manipulation tips and tricks' by Peter Acklam.
0022 % Version 1.0 June 2010
0023 %--------------------------------------------------------------------------
0024 
0025 
0026 %% If x is a vector case
0027 if ( ndims(x)==2 && any( size(x)==1 )==1 ) 
0028     if ( strcmp(dir,'right') == 1 ) || ...
0029        ( strcmp(dir,'rt'  ) == 1 ) || ...
0030        ( strcmp(dir,'down') == 1 ) || ...
0031        ( strcmp(dir,'dn'  ) == 1 )
0032    
0033         newX = x( [ end-k+1:end   1:end-k ] );
0034         
0035     elseif ( strcmp(dir,'left') == 1 ) || ...
0036            ( strcmp(dir,'lt'  ) == 1 ) || ...
0037            ( strcmp(dir,'up'  ) == 1 )       
0038        
0039        newX = x( [ k+1:end   1:k ] );
0040     else
0041         error('Parameter dir is not inputed correctly!');
0042     end
0043     
0044 %% If x is the matrix (including rgb image)
0045 elseif ( ndims(x)==2 || (ndims(x)==3 && any(size(x)==3)==1) )
0046     idx = repmat( {':'}, ndims(x), 1);     % initialize subscripts
0047     if ( strcmp(dir,'right') == 1) || ...
0048        ( strcmp(dir,'rt'   ) == 1)
0049    
0050         dim = 2;                           % shift in the horizontal dir
0051         n = size(x, dim);                  % length along dimension dim
0052         idx{dim} = [n-k+1:n   1:n-k];      % shift right k elements
0053         
0054     elseif ( strcmp(dir,'down') == 1) || ...
0055            ( strcmp(dir,'dn'  ) == 1)
0056        
0057        dim = 1;                             % shift in the vertical dir
0058        n = size(x, dim);                    % length along dimension dim
0059        idx{dim} = [n-k+1:n   1:n-k];        % shift right k elements
0060        
0061     elseif ( strcmp(dir,'left') == 1) || ...
0062            ( strcmp(dir,'lt'  ) == 1)
0063        
0064        dim = 2;                             % shift in the horizontal dir
0065        n = size(x, dim);                    % length along dimension dim
0066        idx{dim} = [k+1:n   1:k];            % shift left k elements
0067         
0068     elseif ( strcmp(dir,'up') == 1 )
0069         
0070         dim = 1;                            % shift in the vertical dir
0071         n = size(x, dim);                   % length along dimension dim
0072         idx{dim} = [k+1:n   1:k];           % shift up k elements
0073     else
0074         error('Parameter dir is not inputed correctly!');
0075     end
0076     
0077     newX = x(idx{:});
0078 
0079 
0080 
0081 else
0082     error('Matrix dimensionality is too large.');
0083 end
0084 
0085 
0086 
0087 
0088 
0089 
0090 
0091 
0092 
0093 
0094 
0095 
0096 
0097 
0098 
0099 
0100 
0101 
0102 
0103 
0104 
0105 
0106 
0107 
0108 
0109 
0110 
0111 
0112 
0113 
0114 
0115 
0116 
0117 
0118 
0119 
0120 
0121 
0122 
0123 end

Generated on Thu 17-Mar-2011 14:45:51 by m2html © 2005