Home > matGeom > geom2d > centroid.m

centroid

PURPOSE ^

CENTROID Compute centroid (center of mass) of a set of points.

SYNOPSIS ^

function center = centroid(varargin)

DESCRIPTION ^

CENTROID Compute centroid (center of mass) of a set of points.

   PTS = centroid(POINTS)
   PTS = centroid(PTX, PTY)
   Computes the ND-dimensional centroid of a set of points. 
   POINTS is an array with as many rows as the number of points, and as
   many columns as the number of dimensions. 
   PTX and PTY are two column vectors containing coordinates of the
   2-dimensional points.
   The result PTS is a row vector with Nd columns.

   PTS = centroid(POINTS, MASS)
   PTS = centroid(PTX, PTY, MASS)
   Computes center of mass of POINTS, weighted by coefficient MASS.
   POINTS is a Np-by-Nd array, MASS is Np-by-1 array, and PTX and PTY are
   also both Np-by-1 arrays.

   Example:
   pts = [2 2;6 1;6 5;2 4];
   centroid(pts)
   ans =
        4     3

   See Also:
   points2d, polygonCentroid

 ---------
 Author: David Legland
 e-mail: david.legland@grignon.inra.fr
 created the 07/04/2003.
 Copyright 2010 INRA - Cepia Software Platform.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function center = centroid(varargin)
0002 %CENTROID Compute centroid (center of mass) of a set of points.
0003 %
0004 %   PTS = centroid(POINTS)
0005 %   PTS = centroid(PTX, PTY)
0006 %   Computes the ND-dimensional centroid of a set of points.
0007 %   POINTS is an array with as many rows as the number of points, and as
0008 %   many columns as the number of dimensions.
0009 %   PTX and PTY are two column vectors containing coordinates of the
0010 %   2-dimensional points.
0011 %   The result PTS is a row vector with Nd columns.
0012 %
0013 %   PTS = centroid(POINTS, MASS)
0014 %   PTS = centroid(PTX, PTY, MASS)
0015 %   Computes center of mass of POINTS, weighted by coefficient MASS.
0016 %   POINTS is a Np-by-Nd array, MASS is Np-by-1 array, and PTX and PTY are
0017 %   also both Np-by-1 arrays.
0018 %
0019 %   Example:
0020 %   pts = [2 2;6 1;6 5;2 4];
0021 %   centroid(pts)
0022 %   ans =
0023 %        4     3
0024 %
0025 %   See Also:
0026 %   points2d, polygonCentroid
0027 %
0028 % ---------
0029 % Author: David Legland
0030 % e-mail: david.legland@grignon.inra.fr
0031 % created the 07/04/2003.
0032 % Copyright 2010 INRA - Cepia Software Platform.
0033 %
0034 
0035 %   HISTORY
0036 %   2009-06-22 support for 3D points
0037 %   2010-04-12 fix bug in weighted centroid
0038 %   2010-12-06 update doc
0039 
0040 
0041 %% extract input arguments
0042 
0043 % use empty mass by default
0044 mass = [];
0045 
0046 if nargin==1
0047     % give only array of points
0048     pts = varargin{1};
0049     
0050 elseif nargin==2
0051     % either POINTS+MASS or PX+PY
0052     var = varargin{1};
0053     if size(var, 2)>1
0054         % arguments are POINTS, and MASS
0055         pts = var;
0056         mass = varargin{2};
0057     else
0058         % arguments are PX and PY
0059         pts = [var varargin{2}];
0060     end
0061     
0062 elseif nargin==3
0063     % arguments are PX, PY, and MASS
0064     pts = [varargin{1} varargin{2}];
0065     mass = varargin{3};
0066 end
0067 
0068 %% compute centroid
0069 
0070 if isempty(mass)
0071     % no weight
0072     center = mean(pts);
0073     
0074 else
0075     % format mass to have sum equal to 1, and column format
0076     mass = mass(:)/sum(mass(:));
0077     
0078     % compute weighted centroid
0079     center = sum(bsxfun(@times, pts, mass), 1);
0080     % equivalent to:
0081     % center = sum(pts .* mass(:, ones(1, size(pts, 2))));
0082 end

Generated on Wed 16-Feb-2022 15:10:47 by m2html © 2003-2019