I found a very elegant way to compute the norm of each row in a $n \times 3$ matrix on Matlab
%M is my nX3 matrix
normatrix = sqrt(diag(M*M'))
How can I prove (not with empiric test), that is correct?
I found a very elegant way to compute the norm of each row in a $n \times 3$ matrix on Matlab
%M is my nX3 matrix
normatrix = sqrt(diag(M*M'))
How can I prove (not with empiric test), that is correct?
Copyright © 2021 JogjaFile Inc.
The $i,j$-th entry of $MM^T$ is the dot product of the $i$-th row of $M$ with the $j$-th column of $M^T$ (which is the $j$-th row of $M$).
So, the $i$-th entry on the diagonal of $MM^T$ is the $i$-th row of $M$ dotted with itself, which is the norm squared of the $i$-th row.
Note that if you're actually wanting to calculate the norm of each row, this is a slow way to do it, cause you'll do the $i$-th row dotted with the $j$-th row for $i \neq j$ as well (this has the complexity of matrix multiplication; if $A$ is m x n, this is $O(m^2n)$ in complexity, roughly (i.e. scales superlinearly in the number of entries in the matrix). A cleaner way is to do
which is $O(mn)$, i.e. scales linearly with the number of entries in the matrix.