Creating Vectors in Matlab; Different Methods and Examples

Creating Vectors in Matlab; Different Methods and Examples

This text shows how to obtain vectors in Matlab and how to create vector variables in Matlab codes. As its name implies, Matlab generally works with the base of matrices. Also, vectors are matrices and you could do various matrice calculations with your created vector in Matlab. Also, you could do mathematical calculations with Matlab® vectors and you could insert the Matlab® vector into mathematical calculations with other matrices, vectors, and individual variables. Moreover, this is much easier in Matlab® coding compared with other software languages. And you will see creating vectors in Matlab is very simple.

Check: Recommended Books to Learn Matlab®

How is The Creation of Vectors in Matlab®?

v = [1 0 0]
d = [1; 2; 3]
z = 1:.5:3
p = pi*[-4:2:4]/4

Firstly, there are a bunch of vector examples in the Matlab® script above, we will talk about their working principles below.

>> v = [1 0 0]
d = [1; 2; 3]
z = 1:.5:3
p = pi*[-4:2:4]/4

v =

     1     0     0


d =

     1
     2
     3


z =

    1.0000    1.5000    2.0000    2.5000    3.0000


p =

   -3.1416   -1.5708         0    1.5708    3.1416

>> 

As you can see the v vector is very basic and shows how to create a vector in Matlab in principle. d vector shows how to create column vectors in Matlab®. You could understand that if you put ‘;’ between vector numbers, this vector will be a column vector like d. The meaning of z vector, ve create the z vector starts with 1, ends with 3, and increment of each element in z vector is 0.5. You could type the first value, increment value, and end value to create a vector by putting ‘:’ between these values.

Finally in p vector, we did a bunch of mathematical calculations by using a vector-like z. As you can see we multiplied an increment vector with pi and divided it with 4.

Examples About Creating Vectors in Matlab

Transpose of a Vector

t = [2 1 7 -2 5 6]'

t =

     2
     1
     7
    -2
     5
     6

Matlab provides various kinds of tools to create vectors of different types. For example, we create a vector in which the Matlab calculated its transpose of it. To calculate a transpose of a vector, just add ‘ to the end of the vector.

Look at the example above. We created a vector that has 6 elements inside it. To calculate the transpose of it, we added a quote at the end of the square brackets. So, if you type the name of the vector, you will see the transposition of it on the screen like above.

Incremental Vector

t = [1:6]

t =

     1     2     3     4     5     6  

You can also define incremental vectors in Matlab. Inside the square brackets above, you just need to type the first and last numbers by putting a semicolon between them. By doing it, you define a vector that has all the values between the values that you enter.

Check the example above. We created a vector that has the first value of 1 and the last value of 6. And if we call this vector again, you can see all the values between 1 and 6 are the separate elements of that vector.

t = [3:.5:5]

t =



    3.0   3.5   4.0   5.5  5.0

This is another method to create incremental vectors in Matlab. Respectively, you type the first value of the vector and you type the increment value. And you type the last value of the vector. If you call this vector, the elements of the vector is created with the elements of this incremental value.

Calling the Elements of a Vector in Matlab

If you create a vector in Matlab, you can call the separate elements of that vector. You can use these separate elements in different calculations.

t(3)

= 4.0

For example, we created a vector with an incremental value. If we need to call the third value of that vector, we can write t(3). This will call that element of that vector.

Substractions and Additions of Vectors in Matlab

You can make different substractions and additions to the vectors that you create in Matlab. Check the examples below.

t = [1:6]
u = [1:6]

t-u 

 = 0 0 0 0 0 0 

t+u

 = 2 4 6 8 10 12

As you see above, we can simply add or subtract the vectors in Matlab easily. All the elements are substracted and added between themselves in Matlab. You can use this feature of Matlab in different calculations.

Multiplications and Divisions of Vectors

We can also multiply and divide the vectors that we create in Matlab with different numbers. You can check the code examples below.

t = [1:6]; 

2*t 

 = 2 4 6 8 10 12 

t/2 

 = 0.5 1 1.5 2 2.5 3

As you see in the example above, we multiplied and divided the vector that we create in Matlab. You can see the replies of the Matlab program on these mathematical calculations.

Above all, besides the creating vector in Matlab, there are various kinds of tools to create and manipulate the vectors to make effective calculations. Check the related articles about vectors in Matlab;

Conclusion

As you see above, there are lots of vector-creating methods and commands in the Matlab programming language. You can make different and complex calculations with the vectors that you create.

So, do not forget to leave your comments and questions below about creating vectors in Matlab below. 

If you want further coding examples about ‘the vector creation in Matlab®, inform us in the comments.

This article is prepared for completely educative and informative purposes. Images used courtesy of Matlab®

Your precious feedbacks are very important to us.

Last Updated:

Author:


Leave a Reply

Your email address will not be published.