## centroid?

Apr 11, 2007 at 1:36pm

# centroid?

This is kind of embarrassing, but can anybody write out, in plain English, the centroid equation? I know there’s a snappy one in Lcent, but I need to do it in java, and I can’t work out how to get Dr. Elsea’s little equation to work… I’m not a mathematician by any stretch of the imagination, so all the equations I’ve found online wind up giving me some funky characters that are truly Greek to me (literally). Ssomething like “sum the list, take the average, and…” as an explanation would be brilliant.

J.

#31309
Apr 11, 2007 at 3:01pm

Hi,

I think it’s like a weighted mean of the list values against its indices.

So…

1) compute the sum of each list element multiplied by its index
(presumably indexing from 1, not zero)

2) compute the sum of the list values

3) Divide (1) by (2)

So, something along the lines of:

float weightedsum = 0;
float sum = 0;

for(int i=0; i< mylist.length; i++)
{
weightedsum += (i+1) * mylist[i];
sum += mylist[i];
}

return (weightedsum / sum);

Owen

jbmaxwell wrote:
> This is kind of embarrassing, but can anybody write out, in plain
> English, the centroid equation? I know there’s a snappy one in Lcent,
> but I need to do it in java, and I can’t work out how to get Dr.
> Elsea’s little equation to work… I’m not a mathematician by any
> stretch of the imagination, so all the equations I’ve found online
> wind up giving me some funky characters that are truly Greek to me
> (literally). Ssomething like “sum the list, take the average, and…”
> as an explanation would be brilliant.
>
> Thanks in advance.
>
> J. _______________________________________________ maxmsp mailing
>

#101587
Apr 11, 2007 at 3:27pm

You rock.

That sounds about right to me… I know it’s supposed to give an interpolated index representing the “centre or gravity” of the list, so it would have to acknowledge the indices (and acknowledge zeros), that is to say, I think you’ve got the idea! I’ll give your code a shot a little later.

Thanks as always, Owen.

J.

#101588
Apr 11, 2007 at 5:47pm

Right, cool. So the only change is that it is indexing from 0 rather than 1. Other than that, you were on the money!

cheers,

J.

#101589
Apr 11, 2007 at 5:50pm

ah, to start at 0 or 1, the age old battle.

#101590
Apr 11, 2007 at 5:59pm

heh… the Romans had it sussed, in that regard. Though they would have had a hell of a time getting fuzzy logic to work!

J.

#101591
Apr 11, 2007 at 7:44pm

But in that case, it makes sense: if you use 0, you ignore the first value.

> ah, to start at 0 or 1, the age old battle.

#101592

You must be logged in to reply to this topic.