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 −

Columns 1 through 7:

          1          3          5          7          9         11         13

 Columns 8 through 10:

         15         17         19
 Columns 1 through 7:

          1          9         25         49         81        121        169

 Columns 8 through 10:

        225        289        361

matlab_vectors.htm

Advertisements