Home > matlabmk > spatial_nearest_neighbors.m

spatial_nearest_neighbors

PURPOSE ^

spatial_nearest_neighbors() - Given a set of electrode coordinates, this

SYNOPSIS ^

function chan_hood=spatial_nearest_neighbors(chanlocs,k)

DESCRIPTION ^

 spatial_nearest_neighbors() - Given a set of electrode coordinates, this 
                       function returns a binary 2D matrix that indicates  
                       which electrodes are close enough to be considered 
                       neighbors. Specifically, the k nearest electrodes 
                       to each electrode are defined as neighbors. For use 
                       with cluster-based permutation tests. This method 
                       was suggested by Manish Saggar is useful when your
                       electrode coordinates are different for each
                       subject or if you want to have relatively equal
                       statistical power across all electrodes. Defining
                       spatial neighbors by a simple distance threshold 
                       (using spatial_neighbors.m) means that electrodes 
                       on the edge of the montage will have fewer 
                       neighbors than others and will thus have less 
                       opportunities to join clusters.

 Usage:
  >>chan_hood=spatial_nearest_neighbors(chanlocs,k);

 Required Inputs:
   chanlocs - An EEGLAB chanlocs structure (e.g., EEG.chanlocs from an EEG
              variable)
   k        - The number of electrodes that will be defined as a neighbor
              of each electrode. The nearest k electrodes are chosen as
              neighbors. In case of ties, the lowest indexed channels are 
              chosen over higher indexed channels. If this happens, you
              should probably use a different value of k or manually
              adjust chan_hood to avoid the issue.

 Outputs:
   chan_hood - A symmetric binary matrix indicating which channels are
               neighbors. If chan_hood(a,b)=1, then Channel A and Channel
               B are nieghbors.

 Notes:
 -See also the function spatial_neighbors.m
 -This function won't work if it produces an asymmetric neighborhood
 index, which appears to be hard to avoid.

 Authors:
 Idea from Manish Saggar
 Written by David Groppe
 Feinstein Institute for Medical Research, 3/2013

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 % spatial_nearest_neighbors() - Given a set of electrode coordinates, this
0002 %                       function returns a binary 2D matrix that indicates
0003 %                       which electrodes are close enough to be considered
0004 %                       neighbors. Specifically, the k nearest electrodes
0005 %                       to each electrode are defined as neighbors. For use
0006 %                       with cluster-based permutation tests. This method
0007 %                       was suggested by Manish Saggar is useful when your
0008 %                       electrode coordinates are different for each
0009 %                       subject or if you want to have relatively equal
0010 %                       statistical power across all electrodes. Defining
0011 %                       spatial neighbors by a simple distance threshold
0012 %                       (using spatial_neighbors.m) means that electrodes
0013 %                       on the edge of the montage will have fewer
0014 %                       neighbors than others and will thus have less
0015 %                       opportunities to join clusters.
0016 %
0017 % Usage:
0018 %  >>chan_hood=spatial_nearest_neighbors(chanlocs,k);
0019 %
0020 % Required Inputs:
0021 %   chanlocs - An EEGLAB chanlocs structure (e.g., EEG.chanlocs from an EEG
0022 %              variable)
0023 %   k        - The number of electrodes that will be defined as a neighbor
0024 %              of each electrode. The nearest k electrodes are chosen as
0025 %              neighbors. In case of ties, the lowest indexed channels are
0026 %              chosen over higher indexed channels. If this happens, you
0027 %              should probably use a different value of k or manually
0028 %              adjust chan_hood to avoid the issue.
0029 %
0030 % Outputs:
0031 %   chan_hood - A symmetric binary matrix indicating which channels are
0032 %               neighbors. If chan_hood(a,b)=1, then Channel A and Channel
0033 %               B are nieghbors.
0034 %
0035 % Notes:
0036 % -See also the function spatial_neighbors.m
0037 % -This function won't work if it produces an asymmetric neighborhood
0038 % index, which appears to be hard to avoid.
0039 %
0040 % Authors:
0041 % Idea from Manish Saggar
0042 % Written by David Groppe
0043 % Feinstein Institute for Medical Research, 3/2013
0044 
0045 %%%%% Future Work %%%%%%
0046 % figure out how to deal with asymmetric
0047 
0048 function chan_hood=spatial_nearest_neighbors(chanlocs,k)
0049 
0050 n_chan=length(chanlocs);
0051 
0052 chan_hood=zeros(n_chan,n_chan);
0053 n_neighbors=zeros(1,n_chan);
0054 chan_dist=zeros(1,n_chan*(n_chan-1)/2);
0055 ct=0;
0056 for c=1:n_chan,
0057     coordA=[chanlocs(c).X chanlocs(c).Y chanlocs(c).Z];
0058     dists=zeros(1,n_chan);
0059     for d=1:n_chan,
0060         coordB=[chanlocs(d).X chanlocs(d).Y chanlocs(d).Z];
0061         dists(d)=sum((coordA-coordB).^2); % Ok to keep distance in units squared
0062     end
0063     [vals ids]=sort(dists);
0064     chan_hood(c,ids(1:k+1))=1; %k+1 is used because each channel should be a neighbor with itself
0065 end
0066 
0067 % Check for symmetry
0068 is_sym=1;
0069 for a=1:n_chan,
0070     for b=a+1:n_chan,
0071         if chan_hood(a,b)~=chan_hood(b,a)
0072            is_sym=0;
0073            break;
0074         end
0075     end
0076 end
0077 
0078 if ~is_sym,
0079     error('Spatial neighborhood matrix is not symmetric. You need to change the value of k, define chan_hood manually, or use spatial_neighbors.m');
0080 end

Generated on Tue 10-May-2016 16:37:45 by m2html © 2005