English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

MATLAB Vector Size

element v1,v2,v3,…,vn the size of the vector v is given by the following formula-

| v | =√(v1 2 + v2 2 + v3 2 +...+ vn 2)

You need to take the following steps to calculate the size of the vector-

  • Usearray multiplication(.*)to take the product of the vector with itself. This will produce a vector sv whose elements are the squares of the elements of the vector v.

    sv = v.* v;

  • Use the sum function to obtainsumThe square of the elements of the vector v. This is also known as the dot product of the vector v.

    dp = sum(sv);

  • UsesqrtThe function obtains the square root of the sum, which is also the size of the vector v.

    mag = sqrt(s);

Online Example

Use the following code to create a script file-

v = [1: 2: 20];
sv = v.* v;  % vector with elements
                  % as the square of v elements
dp = sum(sv);  % sum of squares - dot product
mag = sqrt(dp);  % magnitude
disp('Magnitude:'); 
disp(mag);
When the file is run, it displays the following result-
Magnitude:
36.469