Just a note for clarity in case anyone else is reading this, the trace is the sum of the diagonal elements of A, not all of the elements.
Interestingly, for a reason that is not obvious to me, your solution does in fact give the correct trace though, whereas if we defined some square matrix
B = A*A'
then
trace(B)
is not equal to
sum(identity,B)
so I don’t see why
sum(abs2,A)
does give the right answer.
Edit:
I should have known better than to comment late at night on linear algebra. I completely forgot the product identities
Check sources such as Wikipedia to confirm that the trace of a product X*Y.' can be written as sum(X .* Y). The difference is, you are using sum(identity, B) where B = X*Y.' already. Then, you need to use sum(identity, diag(B)). In the solution provided, he is using A not A*A.', avoiding matrix multiplication and vectorizing the operations.