MATLAB - Vectors with Uniformly Spaced Elements

Advertisements


MATLAB allows you to create a vector with uniformly spaced elements.

To create a vector v with the first element f, last element l, and the difference between elements is any real number n, we write:

v = [f : n : l]

Example

Create a script file with the following code:

v = [1: 2: 20];
sqv = v.^2;
disp(v);disp(sqv);

When you run the file, it displays the following result:

1     3     5     7     9    11    13    15    17    19
1     9    25    49    81   121   169   225   289   361


Advertisements
Advertisements