function [x_m,y_m,z_m]=com(m,xyz)
%computes e position of the Centre Of Mass of n particles
%distributed in space 
%m: nx1 vector of weights [w1;...;wn]
%xyz: nx3 matrix of coordinates [x1,y1,z1;...;xn,yn,zn]
%com makes use of centremass1D.m function

%----------
%example
%>>m=[5;3;9;1];
%>>points=[-1,1,1;3,4,4;8,9,2;0,1,-1];
%>>[x,y,z]=com(m,points)
%x =
%    4.2222
%y =
%    5.5000
%z =
%    1.8889
%---------

if size(m,2)~=1
    disp('error! m must be a nx1 vector of weights [w1;...;wn]')
    x_m=NaN;y_m=NaN;z_m=NaN;
    return
end
if size(xyz,2)~=3
   disp('error! xyz must have 3 columns');
   x_m=NaN;y_m=NaN;z_m=NaN;
   return
end 
if size(xyz,1)~=size(m,1)
   disp('m and xyz must have the same number of rows');
   x_m=NaN;y_m=NaN;z_m=NaN;
   return
end 

x=xyz(:,1);
y=xyz(:,2);
z=xyz(:,3);
x_m=centremass1D(m,x);
y_m=centremass1D(m,y);
z_m=centremass1D(m,z);
end
