Skip to content
This repository has been archived by the owner. It is now read-only.

Commit

Permalink
added the centerofgravity function
Browse files Browse the repository at this point in the history
  • Loading branch information
MPIBR-vollrathf committed Sep 21, 2016
1 parent 9f5231e commit 770e662
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions centerofgravity.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
function cog = centerofgravity(d,thr)
%Determine center of mass of a matrix
% cog = centerofgravity(d,thr)

if nargin == 2
d(d<thr) = 0;
end
d=double(d);



for dim = 1:numel(size(d))
dd = d;
ind = 1:numel(size(d));
ind(dim) = [];
dd = permute(dd,[dim ind]);
dd = reshape(dd,size(d,dim),[]);
dsum=nansum(dd,2);
distance = 1:size(d,dim);
cog(dim) = nansum(dsum(:).*distance(:)) / nansum(dsum);
end

0 comments on commit 770e662

Please sign in to comment.