L_MAGNITUDE
Calculates the magnitude of a vector as SQRT(SUM(x^2))
=L_MAGNITUDE(vector)
Argument | Description | Example |
---|---|---|
vector | A row or column vector of numeric values | 2 |
Required: L_COLSUM
In the template file, navigate to the Matrices worksheet to see the L_MAGNITUDE function in action.
Description
The MAGNITUDE of a vector is the square root of the sum of the squares of the elements. There are a number of different ways to calculate this in Excel when a is a vector:
=L_MAGNITUDE(a) =SQRT(SUM(a*a)) =SQRT(SUM(a^2)) =SQRT(SUMPRODUCT(a,a)) =SQRT(L_DOT(a,a)) =SQRT(SUMSQ(a))
If the matrix A contains multiple columns, then L_MAGNITUDE(A) returns a row vector containing the magnitudes of the individual columns. This is similar in behavior to the vecnorm function in Matlab.
=SQRT(L_COLSUM(A^2))
Lambda Formula
This code for using L_MAGNITUDE in Excel is provided under the License as part of the LAMBDA Library, but to use just this function, you may copy the following code directly into your spreadsheet.
Code to Create Function via the Name Manager
Name: L_MAGNITUDE
Comment: Calculates the magnitude of a vector as SQRT(SUM(x^2))
Refers To:
=LAMBDA(vector,
LET(doc,"https://www.vertex42.com/lambda/magnitude.html",
vec,IF(AND(ROWS(vector)=1,COLUMNS(vector)>1),TRANSPOSE(vector),vector),
IF(COLUMNS(vec)>1,
SQRT(L_COLSUM(vec*vec)),
SQRT(SUM(vec*vec))
)
))
Code for AFE Workbook Module (Excel Labs Add-in)
/** * Returns the magnitude (2-norm) of a vector or the magnitude of each column of a matrix */ L_MAGNITUDE = LAMBDA(vector, LET(doc,"https://www.vertex42.com/lambda/magnitude.html", vec,IF(AND(ROWS(vector)=1,COLUMNS(vector)>1),TRANSPOSE(vector),vector), IF(COLUMNS(vec)>1, SQRT(L_COLSUM(vec*vec)), SQRT(SUM(vec*vec)) ) ));
Named Function for Google Sheets
Name: L_MAGNITUDE Description: Calculates the magnitude of a vector as SQRT(SUM(x^2)) Arguments: vector Function: LET(doc,"https://www.vertex42.com/lambda/magnitude.html", vec,IF(AND(ROWS(vector)=1,COLUMNS(vector)>1),TRANSPOSE(vector),vector), IF(COLUMNS(vec)>1, ARRAYFORMULA(SQRT(L_COLSUM(vec*vec))), ARRAYFORMULA(SQRT(SUM(vec*vec))) ) )
L_MAGNITUDE Examples
Example 1
Calculate the magnitude of each column vector in the given matrix.
Test: Copy and Paste this LET function into a cell =LET( a, {4;-4;2}, b, {0;-3;4}, c, {1;1;1}, matrix, HSTACK(a,b,c), L_MAGNITUDE(matrix) ) Result: {6, 5, 1.732}
Example 2
Check whether a vector is normalized, meaning that the magnitude is equal to 1.
=LET( vector, {0.4934;-0.8654;-0.0873}, L_MAGNITUDE(vector) ) Result: 0.999991Although close to 1, the magnitude is not exactly 1, perhaps due to the original values being rounded or truncated.
Example 3
Normalize a vector by dividing the vector by the magnitude.
=LET( vector, {-3; 0; 4}, vector/L_MAGNITUDE(vector) ) Result: {-0.6; 0; 0.8}
References & Resources
Disclaimer: This article is meant for educational purposes only. See the License regarding the LAMBDA code, and the site Terms of Use for the documentation.