Discussion:
Calculating the Center of Mass of a 3D Binary Array/Matrix
(too old to reply)
Lazyrussian
2007-11-09 18:41:00 UTC
Permalink
Hi,

I'm an undergraduate doing some some physics research. I apologize if
this is the wrong group, but this is the only thing that popped up on
google groups.

Here is my task:

1) Import 10 images into matlab in binary format and superimpose them
on top of each other
2) Put it back into Binary Format (because superimposing 10 images
will yield overlaps and those overlaps values will now contain values
that are greater than 1.
3) Find the 3D center of mass of the volumetric matrix
The object I have is a tumor in 3 Dimensions - The tumor is bounded
within a matrix that is 512,512,117.
The tumor does not compromise the whole of the matrix, just a small
portion.
I thought the math behind finding the Center of Mass (CM) would be
simple but after getting the equations down and pondering over it I
realized that it wouldn't work.

I've finished with steps 1 and 2 (quite easy).

I wanted to do the following (not using the sum matlab function =
sum() represents equals large-sigma with bounds)

A = sum(mx)/M
B = sum(my)/M
C = sum(mz)/M

where m = mass (or the value at the specified coordinate)
where x = the x (or first) dimension
where y = the y (or second) dimension
where z = the z (or third) dimension
where M = total mass over three dimensions (easy to do using a triple
sum function in matlab)
where (A,B,C) = will equal the new coordinates (in decimal format),
but multiplying them by a multiplicative factor shoudl fix that ( a
total guess)

Major problems:
I am keeping the two coordinates that I'm not summing over at a
constant number (i.e. zero)
If I do this, then all I am adding up/summing over are the outer edges
of the matrix which don't contain the tumor (I know this for a fact).

After researching on the internet, I have come to the conclusion that
I am dealing with a centroid, something which I have never seen in my
life.

I'd appreciate any and all help, thank you.
Peter Boettcher
2007-11-09 19:17:23 UTC
Permalink
Post by Lazyrussian
I've finished with steps 1 and 2 (quite easy).
I wanted to do the following (not using the sum matlab function =
sum() represents equals large-sigma with bounds)
A = sum(mx)/M
B = sum(my)/M
C = sum(mz)/M
where m = mass (or the value at the specified coordinate)
where x = the x (or first) dimension
where y = the y (or second) dimension
where z = the z (or third) dimension
where M = total mass over three dimensions (easy to do using a triple
sum function in matlab)
where (A,B,C) = will equal the new coordinates (in decimal format),
but multiplying them by a multiplicative factor shoudl fix that ( a
total guess)
I am keeping the two coordinates that I'm not summing over at a
constant number (i.e. zero)
If I do this, then all I am adding up/summing over are the outer edges
of the matrix which don't contain the tumor (I know this for a fact).
Right. You actually need to sum ALL values for each of the coordinates,
not just a single plane. Think of computing each coordinate (x,y, and
z) independently. Each component actually looks like a triple sum, over
x, and y, and z. On the inside, just as you've written it, is
x*m(x,y,z). This for the x component of the centroid. Then again for
the y and the z.

After that, you can note that when you program it, you can accumulate
all of the components into their own variables in the inner loop.

Finally, in MATLAB, the vectorized version would have you multiply the
entire 3D field with another one that has the value of the x index in
all the right elements. See "meshgrid" to help with that. Then you
just sum all elements in all 3 dimensions. Then do it again for y, and
for z.
Post by Lazyrussian
After researching on the internet, I have come to the conclusion that
I am dealing with a centroid, something which I have never seen in my
life.
Based on your description, you are.

-Peter
Lazyrussian
2007-11-09 21:12:03 UTC
Permalink
Post by Peter Boettcher
Post by Lazyrussian
I've finished with steps 1 and 2 (quite easy).
I wanted to do the following (not using the sum matlab function =
sum() represents equals large-sigma with bounds)
A = sum(mx)/M
B = sum(my)/M
C = sum(mz)/M
where m = mass (or the value at the specified coordinate)
where x = the x (or first) dimension
where y = the y (or second) dimension
where z = the z (or third) dimension
where M = total mass over three dimensions (easy to do using a triple
sum function in matlab)
where (A,B,C) = will equal the new coordinates (in decimal format),
but multiplying them by a multiplicative factor shoudl fix that ( a
total guess)
I am keeping the two coordinates that I'm not summing over at a
constant number (i.e. zero)
If I do this, then all I am adding up/summing over are the outer edges
of the matrix which don't contain the tumor (I know this for a fact).
Right. You actually need to sum ALL values for each of the coordinates,
not just a single plane. Think of computing each coordinate (x,y, and
z) independently. Each component actually looks like a triple sum, over
x, and y, and z. On the inside, just as you've written it, is
x*m(x,y,z). This for the x component of the centroid. Then again for
the y and the z.
After that, you can note that when you program it, you can accumulate
all of the components into their own variables in the inner loop.
Finally, in MATLAB, the vectorized version would have you multiply the
entire 3D field with another one that has the value of the x index in
all the right elements. See "meshgrid" to help with that. Then you
just sum all elements in all 3 dimensions. Then do it again for y, and
for z.
Post by Lazyrussian
After researching on the internet, I have come to the conclusion that
I am dealing with a centroid, something which I have never seen in my
life.
Based on your description, you are.
-Peter
Peter, I've typed up about a 70 line code (with comments). It
calculates the center of mass as if it weren't a plane. I also have 20
lines of code or so that are commented out that sum over the whole
tumor.
I've uploaded it to my site - would you mind checking it out, or
helping me out with the actual coding (I self-taught myself this
language without any manual/help guide), so I only know so much.

Thanks for your help thus far and anymore you're willing to give me!

http://www.lazyrussian.com/center-mass.zip
Peter Boettcher
2007-11-09 22:01:20 UTC
Permalink
Post by Lazyrussian
Post by Peter Boettcher
Post by Lazyrussian
I've finished with steps 1 and 2 (quite easy).
I wanted to do the following (not using the sum matlab function =
sum() represents equals large-sigma with bounds)
A = sum(mx)/M
B = sum(my)/M
C = sum(mz)/M
where m = mass (or the value at the specified coordinate)
where x = the x (or first) dimension
where y = the y (or second) dimension
where z = the z (or third) dimension
where M = total mass over three dimensions (easy to do using a triple
sum function in matlab)
where (A,B,C) = will equal the new coordinates (in decimal format),
but multiplying them by a multiplicative factor shoudl fix that ( a
total guess)
I am keeping the two coordinates that I'm not summing over at a
constant number (i.e. zero)
If I do this, then all I am adding up/summing over are the outer edges
of the matrix which don't contain the tumor (I know this for a fact).
Right. You actually need to sum ALL values for each of the coordinates,
not just a single plane. Think of computing each coordinate (x,y, and
z) independently. Each component actually looks like a triple sum, over
x, and y, and z. On the inside, just as you've written it, is
x*m(x,y,z). This for the x component of the centroid. Then again for
the y and the z.
After that, you can note that when you program it, you can accumulate
all of the components into their own variables in the inner loop.
Finally, in MATLAB, the vectorized version would have you multiply the
entire 3D field with another one that has the value of the x index in
all the right elements. See "meshgrid" to help with that. Then you
just sum all elements in all 3 dimensions. Then do it again for y, and
for z.
Post by Lazyrussian
After researching on the internet, I have come to the conclusion that
I am dealing with a centroid, something which I have never seen in my
life.
Based on your description, you are.
-Peter
Peter, I've typed up about a 70 line code (with comments). It
calculates the center of mass as if it weren't a plane. I also have 20
lines of code or so that are commented out that sum over the whole
tumor.
I've uploaded it to my site - would you mind checking it out, or
helping me out with the actual coding (I self-taught myself this
language without any manual/help guide), so I only know so much.
I took a look, but the code doesn't seem to have a triple sum...

How's this:

data = <your data>
for z=1:size(data,3)
for y=1:size(data,2)
for x=1:size(data,1)
sum_x = sum_x + x*data(x,y,z);
sum_y = sum_y + y*data(x,y,z);
sum_z = sum_z + z*data(x,y,z);
end
end
end

"Vectorizing" left as an exercise to the reader. By the way, the MATLAB
manuals are all available for free online. It might help you to read
through some of those.

-Peter
Lazyrussian
2007-11-10 08:23:12 UTC
Permalink
Post by Peter Boettcher
Post by Lazyrussian
Post by Peter Boettcher
Post by Lazyrussian
I've finished with steps 1 and 2 (quite easy).
I wanted to do the following (not using the sum matlab function =
sum() represents equals large-sigma with bounds)
A = sum(mx)/M
B = sum(my)/M
C = sum(mz)/M
where m = mass (or the value at the specified coordinate)
where x = the x (or first) dimension
where y = the y (or second) dimension
where z = the z (or third) dimension
where M = total mass over three dimensions (easy to do using a triple
sum function in matlab)
where (A,B,C) = will equal the new coordinates (in decimal format),
but multiplying them by a multiplicative factor shoudl fix that ( a
total guess)
I am keeping the two coordinates that I'm not summing over at a
constant number (i.e. zero)
If I do this, then all I am adding up/summing over are the outer edges
of the matrix which don't contain the tumor (I know this for a fact).
Right. You actually need to sum ALL values for each of the coordinates,
not just a single plane. Think of computing each coordinate (x,y, and
z) independently. Each component actually looks like a triple sum, over
x, and y, and z. On the inside, just as you've written it, is
x*m(x,y,z). This for the x component of the centroid. Then again for
the y and the z.
After that, you can note that when you program it, you can accumulate
all of the components into their own variables in the inner loop.
Finally, in MATLAB, the vectorized version would have you multiply the
entire 3D field with another one that has the value of the x index in
all the right elements. See "meshgrid" to help with that. Then you
just sum all elements in all 3 dimensions. Then do it again for y, and
for z.
Post by Lazyrussian
After researching on the internet, I have come to the conclusion that
I am dealing with a centroid, something which I have never seen in my
life.
Based on your description, you are.
-Peter
Peter, I've typed up about a 70 line code (with comments). It
calculates the center of mass as if it weren't a plane. I also have 20
lines of code or so that are commented out that sum over the whole
tumor.
I've uploaded it to my site - would you mind checking it out, or
helping me out with the actual coding (I self-taught myself this
language without any manual/help guide), so I only know so much.
I took a look, but the code doesn't seem to have a triple sum...
data = <your data>
for z=1:size(data,3)
for y=1:size(data,2)
for x=1:size(data,1)
sum_x = sum_x + x*data(x,y,z);
sum_y = sum_y + y*data(x,y,z);
sum_z = sum_z + z*data(x,y,z);
end
end
end
"Vectorizing" left as an exercise to the reader. By the way, the MATLAB
manuals are all available for free online. It might help you to read
through some of those.
-Peter
Thanks for your help. I've actually read up on an easier way of
finding the center of mass using the FIND command to get all the tumor
coordinates in 2D and looping it to get the total in 3D.

Thanks again!
arezoo
2007-11-14 02:45:24 UTC
Permalink
Post by Lazyrussian
Post by Peter Boettcher
On Nov 9, 2:17 pm, Peter Boettcher
Post by Peter Boettcher
Post by Lazyrussian
I've finished with steps 1 and 2 (quite easy).
I wanted to do the following (not using the sum
matlab function =
Post by Lazyrussian
Post by Peter Boettcher
Post by Peter Boettcher
Post by Lazyrussian
sum() represents equals large-sigma with bounds)
A = sum(mx)/M
B = sum(my)/M
C = sum(mz)/M
where m = mass (or the value at the specified
coordinate)
Post by Lazyrussian
Post by Peter Boettcher
Post by Peter Boettcher
Post by Lazyrussian
where x = the x (or first) dimension
where y = the y (or second) dimension
where z = the z (or third) dimension
where M = total mass over three dimensions (easy to
do using a triple
Post by Lazyrussian
Post by Peter Boettcher
Post by Peter Boettcher
Post by Lazyrussian
sum function in matlab)
where (A,B,C) = will equal the new coordinates (in
decimal format),
Post by Lazyrussian
Post by Peter Boettcher
Post by Peter Boettcher
Post by Lazyrussian
but multiplying them by a multiplicative factor
shoudl fix that ( a
Post by Lazyrussian
Post by Peter Boettcher
Post by Peter Boettcher
Post by Lazyrussian
total guess)
I am keeping the two coordinates that I'm not
summing over at a
Post by Lazyrussian
Post by Peter Boettcher
Post by Peter Boettcher
Post by Lazyrussian
constant number (i.e. zero)
If I do this, then all I am adding up/summing over
are the outer edges
Post by Lazyrussian
Post by Peter Boettcher
Post by Peter Boettcher
Post by Lazyrussian
of the matrix which don't contain the tumor (I know
this for a fact).
Post by Lazyrussian
Post by Peter Boettcher
Post by Peter Boettcher
Right. You actually need to sum ALL values for each
of the coordinates,
Post by Lazyrussian
Post by Peter Boettcher
Post by Peter Boettcher
not just a single plane. Think of computing each
coordinate (x,y, and
Post by Lazyrussian
Post by Peter Boettcher
Post by Peter Boettcher
z) independently. Each component actually looks like
a triple sum, over
Post by Lazyrussian
Post by Peter Boettcher
Post by Peter Boettcher
x, and y, and z. On the inside, just as you've
written it, is
Post by Lazyrussian
Post by Peter Boettcher
Post by Peter Boettcher
x*m(x,y,z). This for the x component of the
centroid. Then again for
Post by Lazyrussian
Post by Peter Boettcher
Post by Peter Boettcher
the y and the z.
After that, you can note that when you program it,
you can accumulate
Post by Lazyrussian
Post by Peter Boettcher
Post by Peter Boettcher
all of the components into their own variables in the
inner loop.
Post by Lazyrussian
Post by Peter Boettcher
Post by Peter Boettcher
Finally, in MATLAB, the vectorized version would have
you multiply the
Post by Lazyrussian
Post by Peter Boettcher
Post by Peter Boettcher
entire 3D field with another one that has the value
of the x index in
Post by Lazyrussian
Post by Peter Boettcher
Post by Peter Boettcher
all the right elements. See "meshgrid" to help with
that. Then you
Post by Lazyrussian
Post by Peter Boettcher
Post by Peter Boettcher
just sum all elements in all 3 dimensions. Then do
it again for y, and
Post by Lazyrussian
Post by Peter Boettcher
Post by Peter Boettcher
for z.
Post by Lazyrussian
After researching on the internet, I have come to
the conclusion that
Post by Lazyrussian
Post by Peter Boettcher
Post by Peter Boettcher
Post by Lazyrussian
I am dealing with a centroid, something which I
have never seen in my
Post by Lazyrussian
Post by Peter Boettcher
Post by Peter Boettcher
Post by Lazyrussian
life.
Based on your description, you are.
-Peter
Peter, I've typed up about a 70 line code (with
comments). It
Post by Lazyrussian
Post by Peter Boettcher
calculates the center of mass as if it weren't a
plane. I also have 20
Post by Lazyrussian
Post by Peter Boettcher
lines of code or so that are commented out that sum
over the whole
Post by Lazyrussian
Post by Peter Boettcher
tumor.
I've uploaded it to my site - would you mind checking
it out, or
Post by Lazyrussian
Post by Peter Boettcher
helping me out with the actual coding (I self-taught
myself this
Post by Lazyrussian
Post by Peter Boettcher
language without any manual/help guide), so I only
know so much.
Post by Lazyrussian
Post by Peter Boettcher
I took a look, but the code doesn't seem to have a
triple sum...
Post by Lazyrussian
Post by Peter Boettcher
data = <your data>
for z=1:size(data,3)
for y=1:size(data,2)
for x=1:size(data,1)
sum_x = sum_x + x*data(x,y,z);
sum_y = sum_y + y*data(x,y,z);
sum_z = sum_z + z*data(x,y,z);
end
end
end
"Vectorizing" left as an exercise to the reader. By the
way, the MATLAB
Post by Lazyrussian
Post by Peter Boettcher
manuals are all available for free online. It might
help you to read
Post by Lazyrussian
Post by Peter Boettcher
through some of those.
-Peter
Thanks for your help. I've actually read up on an easier
way of
Post by Lazyrussian
finding the center of mass using the FIND command to get
all the tumor
Post by Lazyrussian
coordinates in 2D and looping it to get the total in 3D.
Thanks again!
Hi

May I know how you are going to find the center? I am
working on a problem which includes finding the center for
data clusters and couldn't find a matlab function to do
that, so I thought it is similar to finding the center of
the mass in 3D. I'd be thankful if you guide me.

Walter Roberson
2007-11-09 19:30:56 UTC
Permalink
Post by Lazyrussian
The object I have is a tumor in 3 Dimensions - The tumor is bounded
within a matrix that is 512,512,117.
The tumor does not compromise the whole of the matrix, just a small
portion.
I thought the math behind finding the Center of Mass (CM) would be
simple but after getting the equations down and pondering over it I
realized that it wouldn't work.
Is the tumor a constant density? If so then you can create an
array which has a 1 everywhere there is tumor, and 0 everywhere else.
Once you have that, the center of mass becomes relatively easy to
calculate with ndgrid and some multiplications and summations.

If the tumor is not constant density then the code becomes
slightly messier.
--
"All is vanity." -- Ecclesiastes
Lazyrussian
2007-11-09 21:18:48 UTC
Permalink
Post by Walter Roberson
Post by Lazyrussian
The object I have is a tumor in 3 Dimensions - The tumor is bounded
within a matrix that is 512,512,117.
The tumor does not compromise the whole of the matrix, just a small
portion.
I thought the math behind finding the Center of Mass (CM) would be
simple but after getting the equations down and pondering over it I
realized that it wouldn't work.
Is the tumor a constant density? If so then you can create an
array which has a 1 everywhere there is tumor, and 0 everywhere else.
Once you have that, the center of mass becomes relatively easy to
calculate with ndgrid and some multiplications and summations.
If the tumor is not constant density then the code becomes
slightly messier.
--
"All is vanity." -- Ecclesiastes
Walter,

I did exactly that. The tumor is constant density (Tumor regions = 1,
non-tumor regions = 0).
I'll go read up ndgrid right now.
Loading...