Home > custom > misc > euklid_invW.m

euklid_invW

PURPOSE ^

PURPOSE: create an inverse euklidean distance spatial weight matrix

SYNOPSIS ^

function W = euklid_invW(wsy,wsx,n)

DESCRIPTION ^

 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,n)
 where: [wsy] x [wsy] is the x and y size of the matrix
        and [n] 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.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function W = euklid_invW(wsy,wsx,n)
0002 % PURPOSE: create an inverse euklidean distance spatial weight matrix
0003 %         (n x m 'moving-window' style matrix with distance to center cell
0004 %         weighted.)
0005 % -------------------------------------------------------------------
0006 % USAGE: W = euklid_invW(wsy,wsx,n)
0007 % where: [wsy] x [wsy] is the x and y size of the matrix
0008 %        and [n] specifies how to normalise the weight matrix.
0009 %        Options for [n] are:
0010 %        1) 'norm' to create a normalised weight matrix (sum of all
0011 %        weights is 1
0012 %        2) 'none' or NA for a non-normalised matrix (default)
0013 % -------------------------------------------------------------------------
0014 % OUTPUTS:
0015 %        [W] a matrix with weights for every cell except center.
0016 % -------------------------------------------------------------------
0017 % NOTES: Weights are the inverse (1/d) of the euklidean distance to
0018 %        the center cell. Center cell weight is zero.
0019 %
0020 % See also: euklid_invW
0021 %
0022 % Felix Hebeler, Geography Dept., University Zurich, March 2006.
0023 if nargin < 2
0024     error('You need at least 2 input arguments for this function');
0025 end
0026 
0027 wsx=floor(wsx/2);
0028 wsy=floor(wsy/2);
0029 
0030 [X,Y] = meshgrid(-wsx:wsx,-wsy:wsy);
0031 W = sqrt(X.^2+Y.^2);
0032 
0033 W(wsx+1,wsy+1)=1; %set center to 1 to avoid division by zero error
0034 W = 1./W; % get inverse distance
0035 W(wsx+1,wsy+1)=0; %set center weight to zero
0036 
0037 if exist('n','var')
0038     if strcmp(n,'norm');
0039         W=W./sum(W(:));  %normalize W
0040     elseif strcmp(n,'none');
0041     else
0042         error('Specified option for n not available. Please specify either [norm] for normalized matrix or leave blank for inverse Euklidean weights.');
0043     end
0044 end

Generated on Tue 24-Feb-2009 19:14:50 by m2html © 2003