im = [ 1 0 0 0 0;
       1 1 1 1 0;
       1 0 0 0 0;
       0 0 0 0 0;
       0 0 0 0 0]
     
%   [rows,cols] = size(im);
% 
%    area = sum(sum(im));
%    meanx = 0;
% 
%    for r = 1:rows
%      for c = 1:cols
%        meanx = meanx + im(r,c)*c;    
%      end
%    end
% 
%    meanx = meanx/area

   [rows,cols] = size(im);
   x = ones(rows,1)*[1:cols]    % Matrix with each pixel set to its x coordinate
   y = [1:rows]'*ones(1,cols)   %   "     "     "    "    "  "   "  y    "

   area = sum(sum(im));
   meanx = sum(sum(double(im).*x))/area;
   