Home > atmlab > handy > fun1Dwrapper.m

fun1Dwrapper

PURPOSE ^

FUN1DWRAPPER Wrapper around function only handle 1D data

SYNOPSIS ^

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

DESCRIPTION ^

 FUN1DWRAPPER   Wrapper around function only handle 1D data

   Applies a "vector operation" on multi-dimensional data. That is, the given
   function is applied on each vector of *X* and the result is assemled for
   a function operating on the complete *X*. The vectors can be extracted
   in dimension (selected by *dim*). The "vector operation" can be a
   function that takes an arbitrary number of input variables.

   Assume that you have a function z = a(x,y) that performs the operation you
   want, but a is poorly implented and allows only x to be vector. The
   function a can be be applied along dimension 2 of a numeric variable X,
   for some y, as
      Z = fun1Dwrapper(X,2,@a,'X',y);
   If a is defined as z = a(y,x) this function is used as
      Z = fun1Dwrapper(X,2,@a,y,'X');

   The function match *funhandle* can take any number of input variables.

   Example: To randomly permute each vector along dimension 1 of X (where each
   vector is permuted seperately): 
      funhandle = @(x) x(randperm(length(x)));
      Z = fun1Dwrapper(X,1,funhandle,'X');

 FORMAT   Y = fun1Dwrapper(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.
                   Should work as dim in e.g. *sum*.
       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 ^

fun1Dwrapper.m

SOURCE CODE ^

0001 % FUN1DWRAPPER   Wrapper around function only handle 1D data
0002 %
0003 %   Applies a "vector operation" on multi-dimensional data. That is, the given
0004 %   function is applied on each vector of *X* and the result is assemled for
0005 %   a function operating on the complete *X*. The vectors can be extracted
0006 %   in dimension (selected by *dim*). The "vector operation" can be a
0007 %   function that takes an arbitrary number of input variables.
0008 %
0009 %   Assume that you have a function z = a(x,y) that performs the operation you
0010 %   want, but a is poorly implented and allows only x to be vector. The
0011 %   function a can be be applied along dimension 2 of a numeric variable X,
0012 %   for some y, as
0013 %      Z = fun1Dwrapper(X,2,@a,'X',y);
0014 %   If a is defined as z = a(y,x) this function is used as
0015 %      Z = fun1Dwrapper(X,2,@a,y,'X');
0016 %
0017 %   The function match *funhandle* can take any number of input variables.
0018 %
0019 %   Example: To randomly permute each vector along dimension 1 of X (where each
0020 %   vector is permuted seperately):
0021 %      funhandle = @(x) x(randperm(length(x)));
0022 %      Z = fun1Dwrapper(X,1,funhandle,'X');
0023 %
0024 % FORMAT   Y = fun1Dwrapper(X,dim,funhandle,varargin)
0025 %
0026 % OUT   Y           Output data.
0027 % IN    X           Input data. Dimensionalities <= 6 are handled.
0028 %       dim         The dimension along which *funhandle* shall be applied.
0029 %                   Should work as dim in e.g. *sum*.
0030 %       funhandle   Function handle.
0031 %       varargin    Function arguments. The vector argument matching *X* is
0032 %                   specified by giving the string 'X*'. See further above.
0033 
0034 % 2007-11-09   Created by Patrick Eriksson.
0035 
0036 
0037 function Y = fun1Dwrapper(X,dim,funhandle,varargin)
0038 %
0039 if nargin < 4
0040   varargin = { 'X' };
0041 end
0042 %
0043 ix = find( strcmp(varargin,'X') );
0044                                                                           %&%
0045                                                                           %&%
0046 %- Check input                                                            %&%
0047 %                                                                         %&%
0048 rqre_nargin( 3, nargin );                                                 %&%
0049 %                                                                         %&%
0050 rqre_datatype( X, @isnumeric );                                           %&%
0051 rqre_alltypes( dim, {@istensor0,@iswhole} );                              %&%
0052 rqre_in_range( dim, 1, 6 );                                               %&%
0053 if dim > dimens(X)                                                        %&%
0054   error( 'The selected dimension (*dim*) must be <= dimens(X).' );        %&%
0055 end                                                                       %&%
0056 %                                                                         %&%
0057 rqre_datatype( X, @isfunction_handle );                                   %&%
0058 %                                                                         %&%
0059 if isempty(ix)                                                            %&%
0060   error( 'One of the varargin arguments must be the string ''X''.' );     %&%
0061 elseif length(ix) > 1                                                     %&%
0062   error( 'Found several ''X'' among the varargin arguments.' );           %&%
0063 end                                                                       %&%
0064 
0065 
0066 %- Move selected dimension to front
0067 %
0068 if dim > 1
0069  X = shiftdim( X, dim-1 );
0070 end
0071 
0072   
0073 for i6 = 1 : size(X,6)
0074 for i5 = 1 : size(X,5)
0075 for i4 = 1 : size(X,4)
0076 for i3 = 1 : size(X,3)
0077 for i2 = 1 : size(X,2)
0078   
0079   varargin{ix} = X(:,i2,i3,i4,i5,i6);
0080   
0081   y = funhandle( varargin{:} );
0082   
0083   if i2 == 1 & i3 == 1 & i4 == 1 & i5 == 1 & i6 == 1
0084     s    = size(X);
0085     s(1) = size(y,1);
0086     Y    = zeros( s );
0087   end
0088   
0089   Y(:,i2,i3,i4,i5,i6) = y;
0090   
0091 end
0092 end
0093 end
0094 end
0095 end
0096 
0097 
0098 %- Re-shift dims
0099 %
0100 if dim > 1
0101   Y = shiftdim( Y, ndims(Y)-dim+1 );
0102 end

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