I want to compute the L2 norm of a n-d matrix. I want to compute this in a single statement, without introducing temporal variables. But it seems I have to, because if I write it like this, it will complains unbalanced parenthesis,
sqrt(sum((A.^2)(:)))
So I have to introduce a temporal matrix B, to write like this
B = A.^2
sqrt(sum(B(:)))
Is there any technique I can use to avoid this? I also found that if a function returns a matrix, I also cannot write like this
(fun(A))(:)
My main concern is why the operator precedence does not work here.
There are several approaches for solving this in one line. One option is to use the reshape function, to reshape A into a vector, as follows:
sqrt(sum(reshape(A,prod(size(A)),1).^2))
Another option is to use Matlab's builtin function:
sqrt(sum(builtin('_paren', A(:)).^2))