Frage

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!

War es hilfreich?

Lösung 2

Use the norm function

B = norm(A,2);

The second parameter indicates you want to use the Euclidean norm

Andere Tipps

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))
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top