I have a 3 columns, n rows matrix:
[ a,b,c; d,e,f; g,h,i; ]
I want to apply the norm function to each of the rows, and get a 1xn
matrix containing the norms:
[ norm([a,b,c]); norm([d,e,f]); norm([g,h,i]); ]
I could do this with a for-loop, but is there a better way?
u = repelem( v , n ) , where v is a scalar or vector, returns a vector of repeated elements of v . If n is a scalar, then each element of v is repeated n times. The length of u is length(v)*n .
vecnorm(A,p,1) calculates the norm of each column. vecnorm(A,p,2) calculates the norm of each row. vecnorm returns abs(A) when dim is greater than ndims(A) or when size(A,dim) is 1 .
What about
norms = sqrt(sum(A.^2,1))
or
norms = sqrt(sum(A.^2,2))?
depending on whether your coordinates are in rows or in columns.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With