Home > lattice > atinsertelems.m

atinsertelems

PURPOSE ^

ATINSERTELEMS Insert elements at given locations in a line

SYNOPSIS ^

function newring=atinsertelems(ring,refpts,varargin)

DESCRIPTION ^

ATINSERTELEMS Insert elements at given locations in a line

NEWLINE=ATINSERTELEMS(LINE,REFPTS,FRAC1,ELEM1[,FRAC2,ELEM2...])
   a new line is created by inserting elements at each location specified
   by REFPTS.

LINE:      Cell array of structures
REFPTS:    Insertion points (index list or logical mask)
FRAC:      Location of the inserted element ELEM within LINE{REFPTS(i)}
           0<=FRAC<=1
 if FRAC = 0, ELEM is inserted before LINE{REFPTS(i)} (no splitting)
 if FRAC = 1, ELEM is inserted after LINE{REFPTS(i)} (no splitting)
 if FRAC = NaN, LINE{REFPTS(i)} is replaced by ELEM (no check for identical length)
 if ELEM = [], nothing is inserted, only the splitting takes place

 FRAC and ELEM must be scalars or array of the same size as REFPTS

 See also ATSPLITELEM ATDIVELEM

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function newring=atinsertelems(ring,refpts,varargin)
0002 %ATINSERTELEMS Insert elements at given locations in a line
0003 %
0004 %NEWLINE=ATINSERTELEMS(LINE,REFPTS,FRAC1,ELEM1[,FRAC2,ELEM2...])
0005 %   a new line is created by inserting elements at each location specified
0006 %   by REFPTS.
0007 %
0008 %LINE:      Cell array of structures
0009 %REFPTS:    Insertion points (index list or logical mask)
0010 %FRAC:      Location of the inserted element ELEM within LINE{REFPTS(i)}
0011 %           0<=FRAC<=1
0012 % if FRAC = 0, ELEM is inserted before LINE{REFPTS(i)} (no splitting)
0013 % if FRAC = 1, ELEM is inserted after LINE{REFPTS(i)} (no splitting)
0014 % if FRAC = NaN, LINE{REFPTS(i)} is replaced by ELEM (no check for identical length)
0015 % if ELEM = [], nothing is inserted, only the splitting takes place
0016 %
0017 % FRAC and ELEM must be scalars or array of the same size as REFPTS
0018 %
0019 % See also ATSPLITELEM ATDIVELEM
0020 
0021 if islogical(refpts),refpts=find(refpts); end
0022 
0023 vargs=cellfun(@unfold,varargin,'UniformOutput',false);
0024 
0025 deb=1;
0026 slices=cellfun(@insert,num2cell(refpts),vargs{:},'UniformOutput',false);  %Split into sections
0027 newring=cat(1,slices{:},ring(deb:end)); %Concatenate all sections
0028 
0029     function slice=insert(idx,varargin)
0030         if isnan(varargin{1})
0031             if iscell(varargin{2})
0032                 slice=[ring(deb:idx-1);varargin{2}];
0033             else
0034                 slice=[ring(deb:idx-1);varargin(2)];
0035             end
0036         else
0037             slice=[ring(deb:idx-1);atsplitelem(ring{idx},varargin{:})];
0038         end
0039         deb=idx+1;
0040     end
0041 
0042     function val=unfold(arg)
0043         if isempty(arg)
0044             val=cell(size(refpts));
0045         elseif isscalar(arg)
0046             val=arg(ones(size(refpts)));
0047         else
0048             val=reshape(arg,size(refpts));
0049         end
0050         if ~iscell(arg)
0051             val=num2cell(val);
0052         end
0053     end
0054 end

Generated on Thu 24-Aug-2017 18:47:33 by m2html © 2005