> Financial Mathematics
>> MATLAB Programming
>>> Vectors in Matlab

Vectors in Matlab

Learn how to create large vectors from starting to end point with equal spaces. Use Linspace to create from starting to end point in different number of elements.


Q 1. Create a one dimensional array (vector) starting from 1 and ending at 9 with spaces 2.


>> A = 1:2:9 (press Enter)
A =
     1 3 5 7 9



Q 2. Generate a vector starting from 3 and ending at 19 with spaces 4.


>> A = 3:4:19 (press Enter)
A =
     3 7 11 15 19


Q 3. Generate a vector from 1 to 10.


>> A = 1:10 (press Enter)
A =
     1 2 3 4 5 6 7 8 9 10

Here is only starting and ending point (1:10). So, if spaces are not given, the default spaces are 1.



Q 4. Generate a vector from 11 to 15.


>> A = 11:15 (press Enter)
A =
     11 12 13 14 15


Q 5. Generate a vector from -3 to 3 with spaces 0.5.


>> A = -3 : 0.5 : 3 (press Enter)
A =
     -3   -2.5   -2   -1.5   -1   -0.5   0   0.5   1   1.5   2   2.5   3


Q 6. Generate a vector from 10 to 1 with spaces 2.


>> A = 10:2:1 (press Enter)
A =
     10 8 6 4 2


Linspace (first element, last element, number of elements)


>> A = linspace (1, 10, 4) {press Enter}
A =
     1 4 7 10



Q 7. Create a vector from 1 to 13 in 7 equal parts.


>> A = linspace (1, 13, 7) {press Enter}
A =
     1 3 5 7 9 11 13


Q 7. Create a vector from 0 to 0.5 in 4 equal parts.


>> A = linspace (0, 0.5, 4) {press Enter}
A =
     0   0.1667   0.3333   0.5000


Next: Matrix Algebra