Home > classes > @ao > index.m

index

PURPOSE ^

INDEX index into an AO array or matrix. This properly captures the history.

SYNOPSIS ^

function varargout = index(varargin)

DESCRIPTION ^

 INDEX index into an AO array or matrix. This properly captures the history.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

 DESCRIPTION: INDEX into an AO array or matrix. This properly captures the
              history. This function is also called when standard () indexing
              is used for AO arrays.

 CALL:        b = index(a, i)
              b = index(a, i, j)

 M-FILE INFO: Get information about this methods by calling
              >> ao.getInfo('index')

              Get information about a specified set-plist by calling:
              >> ao.getInfo('index', 'None')

 VERSION:     $Id: index.m,v 1.17 2008/09/05 11:05:29 ingo Exp $

 HISTORY:     02-05-07 M Hewitson
                 Creation

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 % INDEX index into an AO array or matrix. This properly captures the history.
0002 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0003 %
0004 % DESCRIPTION: INDEX into an AO array or matrix. This properly captures the
0005 %              history. This function is also called when standard () indexing
0006 %              is used for AO arrays.
0007 %
0008 % CALL:        b = index(a, i)
0009 %              b = index(a, i, j)
0010 %
0011 % M-FILE INFO: Get information about this methods by calling
0012 %              >> ao.getInfo('index')
0013 %
0014 %              Get information about a specified set-plist by calling:
0015 %              >> ao.getInfo('index', 'None')
0016 %
0017 % VERSION:     $Id: index.m,v 1.17 2008/09/05 11:05:29 ingo Exp $
0018 %
0019 % HISTORY:     02-05-07 M Hewitson
0020 %                 Creation
0021 %
0022 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0023 
0024 
0025 function varargout = index(varargin)
0026 
0027   % Check if this is a call for parameters
0028   if utils.helper.isinfocall(varargin{:})
0029     varargout{1} = getInfo(varargin{3});
0030     return
0031   end
0032 
0033   if nargout == 0
0034     error('### index cannot be used as a modifier. Please give an output variable.');
0035   end
0036 
0037   import utils.const.*
0038   utils.helper.msg(msg.MNAME, 'running %s/%s', mfilename('class'), mfilename);
0039   
0040   % We can only effectively handle a single matrix of AOs
0041   af = 0;
0042   for j=1:nargin
0043     if isa(varargin{j}, 'ao')
0044       af = af + 1;
0045     end
0046   end
0047   if af > 1
0048     error('### Input a single matrix of AOs for indexing.');
0049   end
0050 
0051   % Collect input variable names
0052   in_names = cell(size(varargin));
0053   for ii = 1:nargin,in_names{ii} = inputname(ii);end
0054 
0055   % Collect all AOs and plists
0056   [as, ao_invars] = utils.helper.collect_objects(varargin(:), 'ao', in_names);
0057   [pl, pl_invars, rest] = utils.helper.collect_objects(varargin(:), 'plist', in_names);
0058 
0059   % Decide on a deep copy or a modify
0060   bs = copy(as, nargout);
0061 
0062   % Combine plists
0063   pl = combine(pl, getDefaultPlist);
0064 
0065   % Get indices
0066   idxi  = find(pl, 'i');
0067   idxj  = find(pl, 'j');
0068   if isempty(idxi) || isempty(idxj)
0069     % go through the other inputs
0070     vals  = [];
0071     for j=1:numel(rest)
0072       if isnumeric(rest{j}) && numel(rest{j})==1
0073         vals = [vals rest{j}];
0074       end
0075     end
0076     if ~isempty(vals)
0077       idxi = vals(1);
0078       if numel(vals) > 1
0079         idxj = vals(2);
0080       end
0081     end
0082   end
0083 
0084   if isempty(idxi) && isempty(idxj)
0085     error('### Please provide an index of index pair.');
0086   end
0087   
0088   % Gather the input histories
0089   inhists = [];
0090   for j=1:numel(bs)
0091     inhists = [inhists bs(j).hist];
0092   end
0093 
0094   % Now index with either (i) or (i,j)
0095   if isempty(idxj)
0096     aout = bs(idxi);
0097     inhists = inhists(idxi);
0098   else
0099     aout = bs(idxi, idxj);
0100     inhists = inhists(sub2ind(size(bs), idxi, idxj));
0101   end
0102 
0103   % Make sure we store the indices
0104   pl = pl.pset('i', idxi);
0105   pl = pl.pset('j', idxj);
0106 
0107   % Name and add history to all outputs
0108   for j=1:numel(aout)
0109     if isempty(idxj)
0110       aout(j).setName(sprintf('%s(%d)', ao_invars{1}, idxi), 'internal');
0111     else
0112       aout(j).setName(sprintf('%s(%d,%d)', ao_invars{1}, idxi, idxj), 'internal');
0113     end
0114     % add history
0115     aout(j).addHistory(getInfo, pl, ao_invars, inhists(j));
0116   end
0117 
0118   % Set output
0119   varargout{1} = aout;
0120 end
0121 
0122 %--------------------------------------------------------------------------
0123 % Get Info Object
0124 %--------------------------------------------------------------------------
0125 function ii = getInfo(varargin)
0126   if nargin == 1 && strcmpi(varargin{1}, 'None')
0127     sets = {};
0128     pl   = [];
0129   else
0130     sets = {'Default'};
0131     pl   = getDefaultPlist;
0132   end
0133   % Build info object
0134   ii = minfo(mfilename, 'ao', '', utils.const.categories.helper, '$Id: index.m,v 1.17 2008/09/05 11:05:29 ingo Exp $', sets, pl);
0135 end
0136 
0137 %--------------------------------------------------------------------------
0138 % Get Default Plist
0139 %--------------------------------------------------------------------------
0140 function pl_default = getDefaultPlist()
0141   pl_default = plist('i',  [], 'j', []);
0142 end
0143

Generated on Mon 08-Sep-2008 13:18:47 by m2html © 2003