function f = cofmass3d(mat3D) %computes the centre of mass in a 3D volume %determine the size of the matrix dimensions=size(mat3D); rows=dimensions(1,1); cols=dimensions(1,2); plan=dimensions(1,3); npoints=sum(sum(sum(mat3D))); %calculate the centre of mass for the x co-ordinates x = 1:rows; y = 1:cols; z = 1:plan; rowtotal=sum(x*mat3D(:,y,z)); coltotal=sum(mat3D(x,:,z)*y'); platotal=sum(mat3D(x,y,:)*z'); f=[rowtotal/npoints coltotal/npoints platotal/npoints];