Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

applying norm function to rows of matrix - Matlab [duplicate]

Tags:

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?

like image 262
AlexBrand Avatar asked Nov 13 '12 17:11

AlexBrand


People also ask

How do you repeat a matrix element in Matlab?

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 .

What does Vecnorm do in Matlab?

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 .


1 Answers

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.

like image 154
Acorbe Avatar answered Oct 05 '22 23:10

Acorbe