Home > atmlab > handy > fun2Dwrapper.m

fun2Dwrapper

PURPOSE ^

FUN2DWRAPPER Wrapper around function only handle 2D data

SYNOPSIS ^

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

DESCRIPTION ^

 FUN2DWRAPPER   Wrapper around function only handle 2D data

   As *fun1dwrapper* but for application of functions operating on
   matrix input.

   The selected dimension (*dim*) will be dimension when calling the
   function of *funhandle*.

   Example: To calculate the sum of "matrix blocks" of A:
      funhandle = @(x) sum(sum(x));
      Z = fun2Dwrapper(X,1,funhandle,'X');

 FORMAT   Y = fun2Dwrapper(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 vector 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 ^

fun2Dwrapper.m

SOURCE CODE ^

0001 % FUN2DWRAPPER   Wrapper around function only handle 2D data
0002 %
0003 %   As *fun1dwrapper* but for application of functions operating on
0004 %   matrix input.
0005 %
0006 %   The selected dimension (*dim*) will be dimension when calling the
0007 %   function of *funhandle*.
0008 %
0009 %   Example: To calculate the sum of "matrix blocks" of A:
0010 %      funhandle = @(x) sum(sum(x));
0011 %      Z = fun2Dwrapper(X,1,funhandle,'X');
0012 %
0013 % FORMAT   Y = fun2Dwrapper(X,dim,funhandle,varargin)
0014 %
0015 % OUT   Y           Output data.
0016 % IN    X           Input data. Dimensionalities <= 6 are handled.
0017 %       dim         The dimension along which *funhandle* shall be applied.
0018 %       funhandle   Function handle.
0019 %       varargin    Function arguments. The vector argument matching *X* is
0020 %                   specified by giving the string 'X*'. See further above.
0021 
0022 % 2007-11-09   Created by Patrick Eriksson.
0023 
0024 
0025 function Y = fun2Dwrapper(X,dim,funhandle,varargin)
0026 %
0027 if nargin < 4
0028   varargin = { 'X' };
0029 end
0030 %
0031 ix = find( strcmp(varargin,'X') );
0032                                                                           %&%
0033                                                                           %&%
0034 %- Check input                                                            %&%
0035 %                                                                         %&%
0036 rqre_nargin( 3, nargin );                                                 %&%
0037 %                                                                         %&%
0038 rqre_datatype( X, @isnumeric );                                           %&%
0039 rqre_alltypes( dim, {@istensor0,@iswhole} );                              %&%
0040 rqre_in_range( dim, 1, 6 );                                               %&%
0041 if dim > dimens(X)                                                        %&%
0042   error( 'The selected dimension (*dim*) must be <= dimens(X).' );        %&%
0043 end                                                                       %&%
0044 %                                                                         %&%
0045 %rqre_datatype( X, @isfunction_handle );                                   %&%
0046 %                                                                         %&%
0047 if isempty(ix)                                                            %&%
0048   error( 'One of the varargin arguments must be the string ''X''.' );     %&%
0049 elseif length(ix) > 1                                                     %&%
0050   error( 'Found several ''X'' among the varargin arguments.' );           %&%
0051 end                                                                       %&%
0052 
0053 
0054 %- Move selected dimension to front
0055 %
0056 if dim > 1
0057  X = shiftdim( X, dim-1 );
0058 end
0059 
0060   
0061 for i6 = 1 : size(X,6)
0062 for i5 = 1 : size(X,5)
0063 for i4 = 1 : size(X,4)
0064 for i3 = 1 : size(X,3)
0065   
0066   varargin{ix} = X(:,:,i3,i4,i5,i6);
0067   
0068   y = funhandle( varargin{:} );
0069   
0070   if i3 == 1 & i4 == 1 & i5 == 1 & i6 == 1
0071     s    = size(X);
0072     s(1) = size(y,1);
0073     s(2) = size(y,2);
0074     Y    = zeros( s );
0075   end
0076   
0077   Y(:,:,i3,i4,i5,i6) = y;
0078   
0079 end
0080 end
0081 end
0082 end
0083 
0084 
0085 %- Re-shift dims
0086 %
0087 if dim > 1
0088   Y = shiftdim( Y, ndims(Y)-dim+1 );
0089 end

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