Sie sind auf Seite 1von 2

function [xi,yi,zi] = griddata(x,y,z,xi,yi,method) %GRIDDATA Data gridding.

% ZI = GRIDDATA(X,Y,Z,XI,YI) returns matrix ZI containing elements % corresponding to the elements of matrices XI and YI and determined by % interpolation within the 2-D function described by the (usually) % nonuniformly-spaced vectors (X,Y,Z). % % XI can be a row vector, in which case it specifies a matrix with % constant columns. Similarly, YI can be a column vector and it % specifies a matrix with constant rows. % % [XI,YI,ZI] = GRIDDATA(X,Y,Z,XI,YI) returns the XI and YI formed % this way, which are the same as the matrices returned by MESHGRID. % % GRIDDATA uses an inverse distance method. % % See also INTERP2, INTERP1. % % % % % % % Copyright (c) 1984-94 by The MathWorks, Inc. $Revision: 5.3 $ Reference: David T. Sandwell, Biharmonic spline interpolation of GEOS-3 and SEASAT altimeter data, Geophysical Research Letters, 2, 139-142, 1987. Describes interpolation using value or gradient of value in any dimension.

error(nargchk(5,5,nargin)) % Ignore method for now. [msg,x,y,z,xi,yi] = xyzchk(x,y,z,xi,yi); if length(msg)>0, error(msg); end xy = x(:) + y(:)*sqrt(-1); % Determine distances between points d = xy(:,ones(1,length(xy))); d = abs(d - d.'); n = size(d,1); % Replace zeros along diagonal with ones (so these don't show up in the % find below or in the Green's function calculation). d(1:n+1:prod(size(d))) = ones(1,n); non = find(d == 0); if ~isempty(non), % If we've made it to here, then some points aren't distinct. Remove % the non-distinct points by averaging. [r,c] = find(d == 0); k = find(r < c); r = r(k); c = c(k); % Extract unique (row,col) pairs v = (z(r) + z(c))/2; % Average non-distinct pairs rep = find(diff(c)==0); if ~isempty(rep), % More than two points need to be averaged. runs = find(diff(diff(c)==0)==1)+1; for i=1:length(runs), k = find(c==c(runs(i))); % All the points in a run v(runs(i)) = mean(z([r(k);c(runs(i))])); % Average (again) end end z(r) = v;

if ~isempty(rep), z(r(runs)) = v(runs); % Make sure average is in the dataset end % Now remove the extra points. x(c) = []; y(c) = []; z(c) = []; xy(c,:) = []; xy(:,c) = []; d(c,:) = []; d(:,c) = []; % Determine the non distinct points ndp = sort([r;c]); ndp(find(ndp(1:length(ndp)-1)==ndp(2:length(ndp)))) = []; disp(sprintf('Averaged %d non-distinct points. Indices are:', ... length(ndp))) disp(ndp') end % Determine weights for interpolation g = (d.^2) .* (log(d)-1); % Green's function. % Fixup value of Green's function along diagonal g(1:size(d,1)+1:prod(size(d))) = zeros(size(d,1),1); weights = g \ z(:); [m,n] = size(xi); zi = zeros(m,n); jay = sqrt(-1); xy = xy.'; % Evaluate at requested points (xi,yi). Loop to save memory. for i=1:m for j=1:n d = abs(xi(i,j)+jay*yi(i,j) - xy); mask = find(d == 0); if length(mask)>0, d(mask) = ones(length(mask),1); end g = (d.^2) .* (log(d)-1); % Green's function. % Value of Green's function at zero if length(mask)>0, g(mask) = zeros(length(mask),1); end zi(i,j) = g * weights; end end if nargout<=1, xi = zi; end

Das könnte Ihnen auch gefallen