1. 程式人生 > >Calculating center of mass of a array IDL

Calculating center of mass of a array IDL

2D

   FUNCTION Centroid, array
   s = Size(array, /Dimensions)
   totalMass = Total(array)
   xcm = Total( Total(array, 2) * Indgen(s[0]) ) / totalMass
   ycm = Total( Total(array, 1) * Indgen(s[1]) ) / totalMass                                                                        
   RETURN, [xcm, ycm]
   END
 
3D
Reasoning by analogy to the 2D case, this should work, I think:

   xcm = Total( Total(Total(array,3),2) * Indgen(s[0])) / totalMass
   ycm = Total( Total(Total(array,3),1) * Indgen(s[1])) / totalMass
   zcm = Total( Total(Total(array,2),1) * Indgen(s[2])) / totalMass