How to perform norm on each row of a matrix?

495 visualizaciones (últimos 30 días)
Hadi Ghahremannezhad
Hadi Ghahremannezhad el 17 de Oct. de 2019
Editada: Brent F el 5 de Jul. de 2021
I have a matrix and each row of the matrix is a vector. I want to perform norm function on each row of this matrix and save the result in a new matrix.
For example, if I had these vectors:
u = [-1 1 0; -1 1 0]
v = [0 1 1; 0 1 -1]
I wanted to calculate the angle between the vectors of the corresponding rows. So the answer should look like this:
angle = [60 ; 60]
But whatever I tried has not worked:
angle = atan2d(norm(cross(u,v,2)),dot(u,v,2))
works for single vectors, but not for a matrix of vectors. This is because I cannot perform the norm function on each row of the two matrices. I also tried this:
angle = atan2d(normr(cross(u,v,2)),dot(u,v,2))
but did not work.
How can I get a result like this:
n = [norm of vectors of first row ; norm of vectors of second row ; norm of vectors of third row ; ...]
witout using a loop?

Respuesta aceptada

John D'Errico
John D'Errico el 17 de Oct. de 2019
What is a norm? I assume by norm, you mean the common Euclidean norm, thus the 2-norm. (Other norms will be just as easy for the most part.)
Could you just use a loop? Of course. But why?
Can you take square all of the elements of your matrix? (I hope so.)
Then it should be easy to sum them, along each row.
Finally, what will the sqrt of that result be? (Hint: the norm that you want.)
In fact, the above will be doable in a fully vectorized form, at least if you did what I suggested.
sqrt(sum(X.^2,2))
When you don't know how to solve a problem, then break it down. Figure out how to solve each component part of the probem. And when all else fails, just use a brute force loop. But expect it to be slow and clumsy, at least loop solution would be so here. But it would trivially work.
  1 comentario
Abhishek Nayak
Abhishek Nayak el 21 de Jul. de 2020
Thanks, Nice one, so easy just need to gve a though. I went for the loop, it was too much time taking.

Iniciar sesión para comentar.

Más respuestas (1)

Steven Lord
Steven Lord el 17 de Oct. de 2019
John's explanation is one way. Another is to use the vecnorm function.
u = [-1 1 0; -1 1 0]
v = [0 1 1; 0 1 -1]
N = vecnorm(cross(u,v,2), 2, 2)
D = dot(u, v, 2)
A = atan2d(N, D)
Take the 2-norm (the second input) in dimension 2 (the third input) of the cross product (the first input) you pass into vecnorm.
  2 comentarios
Hadi Ghahremannezhad
Hadi Ghahremannezhad el 17 de Oct. de 2019
This worked perfectly. But I could only accept one answer. Thank you very much
Brent F
Brent F el 5 de Jul. de 2021
Editada: Brent F el 5 de Jul. de 2021
This is the full solution to your problem. The answer to your question is "Use vecnorm with 2 as arguments 2 and 3": vecnorm(rows, 2, 2).

Iniciar sesión para comentar.

Categorías

Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by