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