performance - Fast way to compute only the diagonal of the square of a matrix -
i have nxm
matrix v
, of compute square s=v'*v
. following computations need diagonal of s
, write s=diag(v'*v)
. however, bit of waste, because i'm computing off-diagonal elements. there fast way compute diagonal elements of s
? use for
loop, of course, explicit looping isn't fast way stuff in matlab.
thanks!!!
that's easy:
sum(conj(v).*v,1)
or
sum(abs(v).^2,1)
if matrix real, can simplify to
sum(v.*v,1)
or
sum(v.^2,1)
Comments
Post a Comment