All pastes #363252 Raw Edit

Something

public text v1 · immutable
·published 2007-02-19 08:26 UTC
rendered paste body
% Function to add all current column with current row, creating an output matrice of the same dimensions, containing the sums.
% Example:
% Input: A = [1 2 3; 4 5 6]
% B(1,1) = 1+2+3(row) + 1+4(column) = 11
% Output: B = [11, 13, 15; 20, 22, 24]

function B = matsum(A)
K=size(A);
B = [];
 
for c=1:K(1) % Loop through rows
   for d=1:K(2) % Loop through columns
      SumC = 0;
      SumR = 0;
      for f=1:K(1) % Add all data in current column?
         SumR = SumR+A(f,d);
      end
      for e=1:K(2) % Add all data in current row
         SumC = SumC+A(c,e);
      end
      B(c,d) = SumR+SumC;
   end
end