function center_of_mass = center_of_mass(x,y) % center_of_mass = get_center_of_mass(x,y) % y can be a matrix or a vector. if size(x,1)1 error('x should be a 1-d array indicating time'); end sizey = size(y); dim_time_y = find(sizey==size(x,1));% which dimension of y equals to time. if dim_time_y ~=2 % time should be the columns dimension y = y'; end if size(y,2)~=size(x,1) error('x and y should have the same length in time'); end center_of_mass = (y*x)./(y*ones(size(x))); end