function [NN_e, NN_d] = elec_distance_nn(dist_e, dist_d)

% elec_distance_nn - Identify distance between nearest neighbour electrodes.
%
% Requires input data generated by 'elec_distance.m'
%
% Usage: [NN_e, NN_d] = elec_distance_nn(dist_e, dist_d)
%
% where: dist_e & dist_d are derived from 'elec_distance'
%        utility and NN_e and NN_d are matrices that contain
%        nearest neighbour electrode labels and distances,
%        respectively.
%

% $Revision: 1.1 $ $Date: 2009-04-28 22:13:54 $

% Licence:  GNU GPL, no implied or express warranties
% History:  05/2000, Darren.Weber_at_radiology.ucsf.edu
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

  fprintf('\n%s\n\n', 'Calculating nearest neighbour electrode distances.');

  NN_e = cell(0);
  NN_d = [];

  for r = 1:length(dist_d(:,1))

       % Sort row r of dist_d in ascending order

       [NN_d(end + 1,:), index] = sort(dist_d(r,:));
		
       % Sort electrode labels in same order

       NN_e(end + 1,:) = dist_e(r,index);

  end
