Home > atmlab > handy > funshiftdim.m

funshiftdim

PURPOSE ^

FUNSHIFTDIM Applies a function along a non-standard dimension

SYNOPSIS ^

function Y = funshiftdim(X,dim,funhandle,varargin)

DESCRIPTION ^

 FUNSHIFTDIM   Applies a function along a non-standard dimension

   Allows the application of a function that only works along a fixed
   dimension. The function can take an arbitrary number of input variables.

   Example: To make a 1D interpolation along dimension 2 of A:
      A = randn(3,5,4);
      Y = funshiftdim(A,2,@interp1,1:5,'X',1.5:1:4.5);

 FORMAT   Y = funshiftdim(X,dim,funhandle,varargin)
        
 OUT   Y           Output data.
 IN    X           Input data. Dimensionalities <= 6 are handled.
       dim         The dimension along which *funhandle* shall be applied.
       funhandle   Function handle.
       varargin    Function arguments. The argument matching *X* is
                   specified by giving the string 'X*'. See further above.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

DOWNLOAD ^

funshiftdim.m

SOURCE CODE ^

0001 % FUNSHIFTDIM   Applies a function along a non-standard dimension
0002 %
0003 %   Allows the application of a function that only works along a fixed
0004 %   dimension. The function can take an arbitrary number of input variables.
0005 %
0006 %   Example: To make a 1D interpolation along dimension 2 of A:
0007 %      A = randn(3,5,4);
0008 %      Y = funshiftdim(A,2,@interp1,1:5,'X',1.5:1:4.5);
0009 %
0010 % FORMAT   Y = funshiftdim(X,dim,funhandle,varargin)
0011 %
0012 % OUT   Y           Output data.
0013 % IN    X           Input data. Dimensionalities <= 6 are handled.
0014 %       dim         The dimension along which *funhandle* shall be applied.
0015 %       funhandle   Function handle.
0016 %       varargin    Function arguments. The argument matching *X* is
0017 %                   specified by giving the string 'X*'. See further above.
0018 
0019 % 2007-11-09   Created by Patrick Eriksson.
0020 
0021 
0022 function Y = funshiftdim(X,dim,funhandle,varargin)
0023 %
0024 if nargin < 4
0025   varargin = { 'X' };
0026 end
0027 %
0028 ix = find( strcmp(varargin,'X') );
0029                                                                           %&%
0030                                                                           %&%
0031 %- Check input                                                            %&%
0032 %                                                                         %&%
0033 rqre_nargin( 3, nargin );                                                 %&%
0034 %                                                                         %&%
0035 rqre_datatype( X, @isnumeric );                                           %&%
0036 rqre_alltypes( dim, {@istensor0,@iswhole} );                              %&%
0037 rqre_in_range( dim, 1, 6 );                                               %&%
0038 if dim > dimens(X)                                                        %&%
0039   error( 'The selected dimension (*dim*) must be <= dimens(X).' );        %&%
0040 end                                                                       %&%
0041 %                                                                         %&%
0042 rqre_datatype( X, @isfunction_handle );                                   %&%
0043 %                                                                         %&%
0044 if isempty(ix)                                                            %&%
0045   error( 'One of the varargin arguments must be the string ''X''.' );     %&%
0046 elseif length(ix) > 1                                                     %&%
0047   error( 'Found several ''X'' among the varargin arguments.' );           %&%
0048 end                                                                       %&%
0049 
0050 
0051 %- Move selected dimension to front
0052 %
0053 if dim > 1
0054  X = shiftdim( X, dim-1 );
0055 end
0056 
0057   
0058 varargin{ix} = X;
0059   
0060 Y = funhandle( varargin{:} );
0061   
0062 
0063 %- Re-shift dims
0064 %
0065 if dim > 1
0066   Y = shiftdim( Y, ndims(Y)-dim+1 );
0067 end

Generated on Mon 15-Sep-2014 13:31:28 by m2html © 2005