center_of_mass.m 612 B

1234567891011121314151617181920
  1. function center_of_mass = center_of_mass(x,y)
  2. % center_of_mass = get_center_of_mass(x,y)
  3. % y can be a matrix or a vector.
  4. if size(x,1)<size(x,2) % number of rows < number of columns
  5. x = x';% now x is a column vector indicating time
  6. end
  7. if size(x,2)>1
  8. error('x should be a 1-d array indicating time');
  9. end
  10. sizey = size(y);
  11. dim_time_y = find(sizey==size(x,1));% which dimension of y equals to time.
  12. if dim_time_y ~=2 % time should be the columns dimension
  13. y = y';
  14. end
  15. if size(y,2)~=size(x,1)
  16. error('x and y should have the same length in time');
  17. end
  18. center_of_mass = (y*x)./(y*ones(size(x)));
  19. end