tp@0: function distances = EDB1calcdist(x1,x2) tp@0: % EDB1calcdist - Gives distances between sets of points in 3D space. tp@0: % EDB1calcdist returns a matrix of distances from all n1 points tp@0: % in x1 to all n2 points in x2. tp@0: % tp@0: % Input parameters: tp@0: % x1 Matrix, [n1,3], with the coordinates of n1 points. tp@0: % x2 Matrix, [n2,3], with the coordinates of n2 points. tp@0: % tp@0: % Output parameters: tp@0: % distances Matrix, [n1,n2], with the distances from all tp@0: % points in x1 to all points in x2. tp@0: % tp@0: % Uses no special functions. tp@0: % tp@0: % ---------------------------------------------------------------------------------------------- tp@0: % This file is part of the Edge Diffraction Toolbox by Peter Svensson. tp@0: % tp@0: % The Edge Diffraction Toolbox is free software: you can redistribute it and/or modify tp@0: % it under the terms of the GNU General Public License as published by the Free Software tp@0: % Foundation, either version 3 of the License, or (at your option) any later version. tp@0: % tp@0: % The Edge Diffraction Toolbox is distributed in the hope that it will be useful, tp@0: % but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS tp@0: % FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. tp@0: % tp@0: % You should have received a copy of the GNU General Public License along with the tp@0: % Edge Diffraction Toolbox. If not, see . tp@0: % ---------------------------------------------------------------------------------------------- tp@0: % Peter Svensson (svensson@iet.ntnu.no) 20001010 tp@0: % tp@0: % distances = EDB1calcdist(x1,x2); tp@0: tp@0: [n11,n12] = size(x1); tp@0: [n21,n22] = size(x2); tp@0: tp@0: if n12 ~= 3 | n22 ~= 3 tp@0: disp('Coordinates must be specified as [x y z]') tp@0: else tp@0: if n11 == 1 tp@0: x1 = x1(ones(n21,1),:); tp@0: d1 = x1-x2; tp@0: distances = sqrt(sum( (d1.').^2 )); tp@0: elseif n21 == 1 tp@0: x2 = x2(ones(n11,1),:); tp@0: d1 = x1-x2; tp@0: distances = sqrt(sum( (d1.').^2 )).'; tp@0: else tp@0: distances = zeros(n11,n21); tp@0: for ii = 1:n21 tp@0: x2point = x2(ii,:); tp@0: x2point = x2point(ones(n11,1),:); tp@0: d1 = x1 - x2point; tp@0: distances(:,ii) = sqrt(sum( (d1.').^2 )).'; tp@0: end tp@0: end tp@0: end tp@0: