IDL: Center of Gravity Function [message #8288] |
Thu, 20 February 1997 00:00  |
Stefan Schoene
Messages: 2 Registered: February 1997
|
Junior Member |
|
|
Hi!
I need a function that returns the index of the center of a
2-dimensional array. But I have to consider the values of the Array.
That means, the function has to return the Center of Gravity of the
Array.
Does anybody know of such a function?
Stefan
|
|
|
Re: IDL: Center of Gravity Function [message #8334 is a reply to message #8288] |
Mon, 24 February 1997 00:00  |
djackson
Messages: 31 Registered: June 1993
|
Member |
|
|
In article <50k9o333p2.fsf@mbcsg1.sghms.ac.uk>, Christian Soeller
<csoelle@sghms.ac.uk> wrote:
> Stefan Schoene <stefan@fritz-haber-institut.mpg.de> writes:
>
>>
>> Hi!
>>
>> I need a function that returns the index of the center of a
>> 2-dimensional array. But I have to consider the values of the Array.
>> That means, the function has to return the Center of Gravity of the
>> Array.
>>
>> Does anybody know of such a function?
>>
>> Stefan
>
> I guess your talking about what is known as the centroid of an image in
> close analogy to the center of mass of a 2D mass distribution.
> I think you can get what you want with
>
> sz = size(array) ; you should probably check that you *do* have a 2D array
> xcoors = indgen(sz(1)) # replicate(1,sz(2))
> ycoors = replicate(1,sz(1)) # indgen(sz(2))
>
> xcg = total(array*xcoors)/total(array)
> ycg = total(array*ycoors)/total(array)
A fine solution, to which I might just add a memory-saver. In this case,
the rows and columns can be totaled separately, giving two 1-D arrays.
With a large starting array, this might be useful.
sz = size(array)
tot = total(array)
xcg = total(total(array,2)*indgen(sz(1)))/tot
ycg = total(total(array,1)*indgen(sz(2)))/tot
Hope this helps!
Cheers,
-Dick
Dick Jackson djackson@ibd.nrc.ca Institute for Biodiagnostics
Opinions are mine alone. National Research Council Canada, Winnipeg
"And I told him my dream was to live for all time
In some perfect refrain, like the man who wrote 'Danny Boy'."
- Joe Jackson, from the album _Night_Music_, 1994.
|
|
|