Use the norm
function
B = norm(A,2);
The second parameter indicates you want to use the Euclidean norm
문제
If I have a vector:
A=[1,1,1]
I know it has length, SQRT( (1^2)+(1^2)+(1^2) ) = SQRT(3) = approx 1.73
But how do I do this in MATLAB?
I have tried:
abs(A)
But this just returns the absolute value of each element of the array. So it just returns the same array, as shown below:
B=abs(A)
B=[1,1,1]
How do I get MATLAB to give me the absolute value of the whole vector, so I get a scalar output?
I would rather do it with a single function instead of operating on each of the elements in the vector individually, as my code is becoming quite messy.
Thanks!
해결책 2
Use the norm
function
B = norm(A,2);
The second parameter indicates you want to use the Euclidean norm
다른 팁
The simplest way is to use norm
:
norm(A)
You could also do it manually: raise each vector element to the power of 2, sum all results to get a single number, and compute its square root:
sqrt(sum(A.^2))