Reputation: 67
I have two images C
and B
and I need to sum the total difference A = C-B
.
Now, I got the solution sum(A)
, but I also read that sum(sum(A))
has also been used for a rating function. Can someone explain me what the difference is?
Upvotes: 0
Views: 691
Reputation: 60680
The function sum
sums over the first non-singleton dimension of the matrix. A singleton dimension is one of size 1. So for a 2D matrix, it will sum over columns, yielding a row vector. Calling sum
with this result will sum over the one row, yielding a single value.
Thus, sum(sum(A))
is the sum over the full 2D matrix A
.
The same can be done with sum(A(:))
, or, in more recent versions of MATLAB, with sum(A,'all')
.
Upvotes: 7