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

猜你喜欢

转载自blog.csdn.net/colddie/article/details/81256549