This is a static copy of a profile report

Home

interp2 (189 calls, 6.499 sec)
Generated 05-Nov-2014 07:52:39 using cpu time.
function in file /usr1/opt/matlab/7.13/toolbox/matlab/polyfun/interp2.m
Copy to new window for comparing multiple runs

Parents (calling functions)

Function NameFunction TypeCalls
ml_downsizefunction189
Lines where the most time was spent

Line NumberCodeCallsTotal Time% TimeTime Plot
153
if (size(x,2)>1 && ...
1892.924 s45.0%
135
[msg,x,y,z,xi,yi] = xyzchk(x,y...
1892.333 s35.9%
217
zi = linear(ExtrapVal,x,y,z,xi...
1891.122 s17.3%
145
if ~isempty(msg)
1890.030 s0.5%
165
xdiff = max(abs(diff(dx))); if...
1890.020 s0.3%
All other lines  0.070 s1.1%
Totals  6.499 s100% 
Children (called functions)

Function NameFunction TypeCallsTotal Time% TimeTime Plot
xyzchkfunction1892.303 s35.4%
repmatfunction3782.203 s33.9%
interp2>linearsubfunction1891.102 s16.9%
Self time (built-ins, overhead, etc.)  0.891 s13.7%
Totals  6.499 s100% 
Code Analyzer results
No Code Analyzer messages.
Coverage results
[ Show coverage for parent directory ]
Total lines in function243
Non-code lines (comments, blank lines)92
Code lines (lines that can run)151
Code lines that did run31
Code lines that did not run120
Coverage (did run/can run)20.53 %
Function listing
   time   calls  line
1 function zi = interp2(varargin)
2 %INTERP2 2-D interpolation (table lookup).
3 % ZI = INTERP2(X,Y,Z,XI,YI) interpolates to find ZI, the values of the
4 % underlying 2-D function Z at the points in matrices XI and YI.
5 % Matrices X and Y specify the points at which the data Z is given.
6 %
7 % XI can be a row vector, in which case it specifies a matrix with
8 % constant columns. Similarly, YI can be a column vector and it
9 % specifies a matrix with constant rows.
10 %
11 % ZI = INTERP2(Z,XI,YI) assumes X=1:N and Y=1:M where [M,N]=SIZE(Z).
12 % ZI = INTERP2(Z,NTIMES) expands Z by interleaving interpolates between
13 % every element, working recursively for NTIMES. INTERP2(Z) is the
14 % same as INTERP2(Z,1).
15 %
16 % ZI = INTERP2(...,METHOD) specifies alternate methods. The default
17 % is linear interpolation. Available methods are:
18 %
19 % 'nearest' - nearest neighbor interpolation
20 % 'linear' - bilinear interpolation
21 % 'spline' - spline interpolation
22 % 'cubic' - bicubic interpolation as long as the data is
23 % uniformly spaced, otherwise the same as 'spline'
24 %
25 % For faster interpolation when X and Y are equally spaced and monotonic,
26 % use the syntax ZI = INTERP2(...,*METHOD).
27 %
28 % ZI = INTERP2(...,METHOD,EXTRAPVAL) specificies a method and a scalar
29 % value for ZI outside of the domain created by X and Y. Thus, ZI will
30 % equal EXTRAPVAL for any value of YI or XI which is not spanned by Y
31 % or X respectively. A method must be specified for EXTRAPVAL to be used,
32 % the default method is 'linear'.
33 %
34 % All the interpolation methods require that X and Y be monotonic and
35 % plaid (as if they were created using MESHGRID). If you provide two
36 % monotonic vectors, interp2 changes them to a plaid internally.
37 % X and Y can be non-uniformly spaced.
38 %
39 % For example, to generate a coarse approximation of PEAKS and
40 % interpolate over a finer mesh:
41 % [x,y,z] = peaks(10); [xi,yi] = meshgrid(-3:.1:3,-3:.1:3);
42 % zi = interp2(x,y,z,xi,yi); mesh(xi,yi,zi)
43 %
44 % Class support for inputs X, Y, Z, XI, YI:
45 % float: double, single
46 %
47 % See also INTERP1, INTERP3, INTERPN, MESHGRID, TriScatteredInterp.
48
49 % Copyright 1984-2011 The MathWorks, Inc.
50 % $Revision: 5.33.4.24 $ $Date: 2011/05/17 02:32:27 $
51
0.01 189 52 error(nargchk(1,7,nargin,'struct')); % allowing for an ExtrapVal
53
189 54 bypass = false;
189 55 uniform = true;
189 56 if (nargin > 1)
189 57 if nargin == 7 && ~isnumeric(varargin{end})
58 error(message('MATLAB:interp2:extrapvalNotNumeric'));
59 end
189 60 if ischar(varargin{end})
61 narg = nargin-1;
62 method = [varargin{end} ' ']; % Protect against short string.
63 if strncmpi(method,'s',1) || strncmpi(method, '*s', 2)
64 ExtrapVal = 'extrap'; % Splines can extrapolate
65 else
66 ExtrapVal = nan; % setting default ExtrapVal as NAN
67 end
68 index = 1; %subtract off the elements not in method
189 69 elseif ischar(varargin{end-1}) && isnumeric(varargin{end})
189 70 narg = nargin-2;
189 71 method = [ varargin{end-1} ' '];
189 72 ExtrapVal = varargin{end}; % user specified ExtrapVal
189 73 index = 2; % subtract off the elements not in method and ExtrapVal
74 else
75 narg = nargin;
76 method = 'linear';
77 ExtrapVal = nan; % protecting default
78 index = 0;
79 end
0.01 189 80 if strncmpi(method,'*',1) % Direct call bypass.
81 if (narg ==5 || narg ==3)
82 xitemp = varargin{end-index - 1};
83 yitemp = varargin{end-index};
84 if isrow(xitemp) && iscolumn(yitemp)
85 varargin{end-index - 1} = repmat(xitemp, [size(yitemp,1), 1]);
86 varargin{end-index} = repmat(yitemp, [1, size(xitemp,2)]);
87 elseif iscolumn(xitemp) && isrow(yitemp)
88 varargin{end-index - 1} = repmat(xitemp', [size(yitemp, 2), 1]);
89 varargin{end-index} = repmat(yitemp', [1, size(xitemp,1)]);
90 end
91 end
92 if strcmpi(method(2),'l') || strcmpi(method(2:4),'bil')
93 % bilinear interpolation.
94 zi = linear(ExtrapVal, varargin{1:end-index});
95 return
96 elseif strcmpi(method(2),'c') || strcmpi(method(2:4),'bic')
97 % bicubic interpolation
98 zi = cubic(ExtrapVal, varargin{1:end-index});
99 return
100 elseif strcmpi(method(2),'n')
101 % Nearest neighbor interpolation
102 zi = nearest(ExtrapVal, varargin{1:end-index});
103 return
104 elseif strcmpi(method(2),'s')
105 % spline interpolation
106 method = 'spline'; bypass = true;
107 else
108 error(message('MATLAB:interp2:InvalidMethod', deblank( method )));
109 end
0.01 189 110 elseif strncmpi(method,'s',1), % Spline interpolation
111 method = 'spline'; bypass = true;
112 end
113 else
114 narg = nargin;
115 method = 'linear';
116 ExtrapVal = nan; % default ExtrapVal is NaN
117 end
118
189 119 if narg==1, % interp2(z), % Expand Z
120 [nrows,ncols] = size(varargin{1});
121 xi = 1:.5:ncols; yi = (1:.5:nrows)';
122 x = 1:ncols; y = 1:nrows;
123 [msg,x,y,z,xi,yi] = xyzchk(x,y,varargin{1},xi,yi);
124
189 125 elseif narg==2. % interp2(z,n), Expand Z n times
126 [nrows,ncols] = size(varargin{1});
127 ntimes = floor(varargin{2}(1));
128 xi = 1:1/(2^ntimes):ncols; yi = (1:1/(2^ntimes):nrows)';
129 x = 1:ncols; y = 1:nrows;
130 [msg,x,y,z,xi,yi] = xyzchk(x,y,varargin{1},xi,yi);
131
189 132 elseif narg==3, % interp2(z,xi,yi)
189 133 [nrows,ncols] = size(varargin{1});
189 134 x = 1:ncols; y = 1:nrows;
2.33 189 135 [msg,x,y,z,xi,yi] = xyzchk(x,y,varargin{1:3});
136
137 elseif narg==4,
138 error(message('MATLAB:interp2:nargin'));
139
140 elseif narg==5, % linear(x,y,z,xi,yi)
141 [msg,x,y,z,xi,yi] = xyzchk(varargin{1:5});
142
143 end
144
0.03 189 145 if ~isempty(msg)
146 error(message(msg.identifier));
147 end
148
149 %
150 % Check for plaid data.
151 %
0.01 189 152 xx = x(1,:); yy = y(:,1);
2.92 189 153 if (size(x,2)>1 && ~isequal(repmat(xx,size(x,1),1),x)) || ...
154 (size(y,1)>1 && ~isequal(repmat(yy,1,size(y,2)),y)),
155 error(message('MATLAB:interp2:meshgrid'));
156 end
157
158 %
159 % Check for non-equally spaced data. If so, map (x,y) and
160 % (xi,yi) to matrix (row,col) coordinate system.
161 %
189 162 if ~bypass,
189 163 xx = xx.'; % Make sure it's a column.
0.01 189 164 dx = diff(xx); dy = diff(yy);
0.02 189 165 xdiff = max(abs(diff(dx))); if isempty(xdiff), xdiff = 0; end
189 166 ydiff = max(abs(diff(dy))); if isempty(ydiff), ydiff = 0; end
189 167 if (xdiff > eps(class(xx))*max(abs(xx))) || (ydiff > eps(class(yy))*max(abs(yy)))
168 if any(dx < 0), % Flip orientation of data so x is increasing.
169 x = fliplr(x); y = fliplr(y); z = fliplr(z);
170 xx = flipud(xx); dx = -flipud(dx);
171 end
172 if any(dy < 0), % Flip orientation of data so y is increasing.
173 x = flipud(x); y = flipud(y); z = flipud(z);
174 yy = flipud(yy); dy = -flipud(dy);
175 end
176
177 if any(dx<=0) || any(dy<=0),
178 error(message('MATLAB:interp2:XorYNotMonotonic'));
179 end
180
181 % Bypass mapping code for cubic
182 if ~strncmp(method(1),'c',1)
183 % Determine the nearest location of xi in x
184 [xxi,j] = sort(xi(:));
185 [~,i] = sort([xx;xxi]);
186 ui(i) = 1:length(i);
187 ui = (ui(length(xx)+1:end)-(1:length(xxi)))';
188 ui(j) = ui;
189
190 % Map values in xi to index offset (ui) via linear interpolation
191 ui(ui<1) = 1;
192 ui(ui>length(xx)-1) = length(xx)-1;
193 ui = ui + (xi(:)-xx(ui))./(xx(ui+1)-xx(ui));
194
195 % Determine the nearest location of yi in y
196 [yyi,j] = sort(yi(:));
197 [~,i] = sort([yy;yyi(:)]);
198 vi(i) = 1:length(i);
199 vi = (vi(length(yy)+1:end)-(1:length(yyi)))';
200 vi(j) = vi;
201
202 % Map values in yi to index offset (vi) via linear interpolation
203 vi(vi<1) = 1;
204 vi(vi>length(yy)-1) = length(yy)-1;
205 vi = vi + (yi(:)-yy(vi))./(yy(vi+1)-yy(vi));
206
207 [x,y] = meshgrid(ones(class(x)):size(x,2),ones(class(y)):size(y,1));
208 xi(:) = ui; yi(:) = vi;
209 else
210 uniform = false;
211 end
212 end
189 213 end
214
215 % Now do the interpolation based on method.
189 216 if strncmpi(method,'l',1) || strncmpi(method,'bil',3) % bilinear interpolation.
1.12 189 217 zi = linear(ExtrapVal,x,y,z,xi,yi);
218
219 elseif strncmpi(method,'c',1) || strncmpi(method,'bic',3) % bicubic interpolation
220 if uniform
221 zi = cubic(ExtrapVal,x,y,z,xi,yi);
222 else
223 zi = spline2(x,y,z,xi,yi,ExtrapVal);
224 end
225
226 elseif strncmpi(method,'n',1) % Nearest neighbor interpolation
227 zi = nearest(ExtrapVal,x,y,z,xi,yi);
228
229 elseif strncmpi(method,'s',1) % Spline interpolation
230 % A column is removed from z if it contains a NaN.
231 % Orient to preserve as much data as possible.
232 [inan, jnan] = find(isnan(z));
233 ncolnan = length(unique(jnan));
234 nrownan = length(unique(inan));
235 if ncolnan > nrownan
236 zi = spline2(y',x',z',yi,xi,ExtrapVal);
237 else
238 zi = spline2(x,y,z,xi,yi,ExtrapVal);
239 end
240 else
241 error(message('MATLAB:interp2:InvalidMethod', deblank( method )));
242
243 end

Other subfunctions in this file are not included in this listing.