function [a,b] = find_com(x,y,A) %x = checkx; y = checky; A = roi_basic; %nargin = 1; x = roi_new; % Finds centre of mass of matrix A in x, y index notation if nargin == 1 A = x; [Ar Ac] = size(A); x = 1:Ac; y = (1:Ar)'; else y = y(:); x = (x(:))'; end sumA = sum(A(:)); if sumA == 0 a = 0; b = 0; else [row col] = size(A); % Find centroid across columns: temp = A.*(ones(row,1)*x); a = sum(temp(:))/sumA; % Across rows: temp = A.*(y*ones(1,col)); b = sum(temp(:))/sumA; end