How to create vectors and matrices in Matlab | applied electronics engineering


How to create vectors and matrices in Matlab

By Applied Electronics - Thursday, February 23, 2017 No Comments
Matlab is both a high programming language and also a software with which you can perform vector and matrix mathematical calculation easily. The power of matlab lies in its ability to work with matrices. In this tutorial we show various ways of creating vectors and matrices in Matlab. These are foundation that are required to be understood to build complex matrix algorithm in Matlab.

Creating Vectors in Matlab

To create a row vector you use the square bracket and enter the elements of the vector into it. The elements can be separated by space or a comma.

To create a column vector there are several techniques. In all the square bracket is used. The first method uses the square bracket to include the elements and a semicolon to separate the elements(no space). The second method is to use the transpose operator ' to the row vector like the one in the example above. See the following.

If you have lots of vector elements arranged in ascending or descending orders, the colon operator can be used instead of typing all the elements. For example if you want to create vector elements from 2 to 6 inclusive and spaced regularly with step value 1 then you can write the following,

If you want vector elements from 1 to 9 with step value of 2 then write as follows.

The linspace function

Another useful function for creating regularly spaced row vector is the linspace function. This can be used for creating time line vector for plotting sine wave or other functions that needs time vector. The linspace accepts two arguments by default which are the first and last element of the vector. The spacing is such that the 100 intervals between the given number is automatically by Matlab. But you can also specify the number of elements between the two end points by specifying third argument. For example, to create a row vector between 3 and 15 with 5 elements you would write as follows,

Creating Matrix in Matlab

The rule of creating matrix is the using the combination of how we create rows and columns in matlab. Rows were created by specifying elements of the row vector seperated by space or comma while column were created by specifying elements of the column vector seperated by semicolon. A matrix is created by specifying rows using the space or comma between the elements of the matrix row and then the next row is created by using semicolon which is like a creating a new column.

For example a 2 by 3 matrix variable mat  is created as follows.

Another way to create new row in a matrix in Matlab is to hit the enter key once one row is completed.

Using colon operator for creating elements of a matrix

You can also use colon operator for creating matrix element which are regularly spaced as shown in the example below.

Using builtin functions to create matrix

There are several built in function to create matrix in Matlab. Some of the regularly used and useful are:

a. rand()
b. zeros()
c. ones()
d. randi()

For example to create a random matrix of size 2 by 2 we can use rand() function as follows.

To create specific row and column of random matrix using rand() function, the first argument is number of rows and the second argument is number of columns. For example to create 1 row and 3 column matrix using rand() function we would write;

This rand() function produces random number from 0 to 1. But what if you want to generate random integers matrix? In that case you can use the randi() function. For example to create a matrix filled with random integers from 5 to 10 and size 2 by 2 we write,

To create matrix filled with random integers from 10 to 13(inclusive) and matrix size of 2 by 3 we write;

The zeros and ones are other builtin functions to create matrices in matlab that you will find useful and constantly using when you develop matlab programs. They work same as rand() function except that when using zeros all the elements of the matrix are 0 and when using ones() function all the elements are 1. They are useful for initialization or for creating sparse matrices which are used in many programming algorithms.

To create a zero matrix of size 3 by 3 you would write,

Similarly you could create all ones matrix of square matrix.

But you could also have rectangular matrix by specifying the number of rows and columns. For example to create a matrix of 2 rows and 4 columns you would write,

These are simple ways to create vectors and matrices in Matlab. There are also other ways you can write matrix elements like using for loop and others. In the next matlab tutorial we will show how to access matrix elements so that you can use them for further manipulation or just for changing the matrix element. These simple vectors and matrices creation are required to understand ecomplex numerical methods using Matlab for sophisticated scientific or algorithmic programs.

No Comment to " How to create vectors and matrices in Matlab "