from
Euklid (Inverse) Distance Weight Matrix
by Felix Hebeler
Calculates a weight matrix of any size using the (inverse) euklidean distance from center cell.
|
| euklid_invW(ws,d,n)
|
function W = euklid_invW(ws,d,n)
% PURPOSE: create an inverse euklidean distance spatial weight matrix
% (n x m 'moving-window' style matrix with distance to center cell
% weighted.)
% -------------------------------------------------------------------
% USAGE: W = euklid_invW([wsy wsx],[dy dx],n)
% where: ws is a vector [wsy wsx] of the x and y size of the matrix (uneven!)
% d (optional) is a vector [dy dx] of the cellspacing in y and x direction
% (default 1)
% [n] (optional) specifies how to normalise the weight matrix.
% Options for [n] are:
% 1) 'norm' to create a normalised weight matrix (sum of all
% weights is 1
% 2) 'none' or NA for a non-normalised matrix (default)
% -------------------------------------------------------------------------
% OUTPUTS:
% [W] a matrix with weights for every cell except center.
% -------------------------------------------------------------------
% NOTES: Weights are the inverse (1/d) of the euklidean distance to
% the center cell. Center cell weight is zero.
%
% See also: euklid_invW
%
% Felix Hebeler, Geography Dept., University Zurich, March 2006.
if nargin < 1
error('You need at least 1 input argument for this function');
end
if exist('d','var')
if size(d,2)~=2 % check if 2nd arg is d or n
n=d;
d=[1 1];
end
else
d=[1 1];
end
ws(1)=floor(ws(1)/2);
ws(2)=floor(ws(2)/2);
[X,Y] = meshgrid(-ws(2):ws(2),-ws(1):ws(1));
X=X*d(2);
Y=Y*d(1);
W = sqrt(X.^2+Y.^2);
W(ws(2)+1,ws(1)+1)=1; %set center to 1 to avoid division by zero error
W = 1./W; % get inverse distance
W(ws(2)+1,ws(1)+1)=0; %set center weight to zero
if exist('n','var')
if strcmp(n,'norm');
W=W./sum(W(:)); %normalize W
elseif strcmp(n,'none');
else
error('Specified option for n not available. Please specify either [norm] for normalized matrix or leave blank for inverse Euklidean weights.');
end
end
|
|
Contact us at files@mathworks.com