In the previous session, we learned how to define arrays in MATLAB of different sizes. The problem, however, is that defining an array as a previous session (ie entering numbers manually) is appropriate for small arrays. Suppose we want to create a 100 × 100 matrix. In this case, we have to enter 10,000 numbers manually! In this session, we want to introduce you to ways to make arrays easier in MATLAB. MATLAB contains many functions that help you create different matrices such as random matrix, unit matrix, zero matrix, identity matrix, and so on.
The zeros command in MATLAB
The first command of special matrices in MATLAB is assigned to the zero matrix. As you know, the word zeros means zeros. In MATLAB, the zeros command generates a matrix with all zero values. Quite simply, with zeros, we can generate a zero matrix in MATLAB. But how to use the zeros command in MATLAB? You must enter the size of the matrix you want as input to this command. To do this, open parenthesis in front of the word zeros. The first number you enter after parentheses for MATLAB indicates how many rows the matrix has. Then put a comma and enter the second number. The second number indicates how many columns the matrix has. For example, in the following command, a zero matrix with 3 rows and 2 columns is created:
>> zeros(3, 2) ans = 0 0 0 0 0 0
With the zeros command in MATLAB, 3D and higher arrays can also be created. For now, it is enough to know that this is possible. You are still the first way. You do not need to immerse yourself in a higher dimension matrix right now.
Is it possible to define a zero vector with zeros in MATLAB? For example, what do you do if you want to define a column vector with 3 zero elements? Do you write zeros (3)? of course not. You definitely write zeros (3, 1) to define the mentioned matrix. Because you already learned that a matrix has a column with 3 elements, 3 rows and a column. So be careful, it doesn’t matter if you want to define a matrix or a vector. In any case, you must specify both rows and columns. For example, for a zero-line vector, length 5 must be written:
>> zeros(1, 5) ans = 0 0 0 0 0
Remember, the first number in parentheses is equal to the number of rows and the second number is equal to the number of columns.
Remember, in MATLAB, the row always precedes the column. First row later column…
So now what if we give input to zeros, for example, write zeros (3)? Do we get an error? Let’s try:
>> zeros(2) ans = 0 0 0 0
You can see that the result of executing the above command is a square matrix that has 2 rows and 2 columns! That is, zeros (2) and zeros (2, 2) must have the same result. that’s mean:
>> zeros(2) ans = 0 0 0 0 >> zeros(2, 2) ans = 0 0 0 0
So, dear MATLAB has made things easier for us. To define a square matrix in MATLAB, we do not need to repeat a number twice. It is enough to write it once. This is why one input cannot be used to define vectors. Because if the zeros command takes an input, it creates a square matrix with it and nothing else!
Reminder A square matrix is a matrix whose number of rows and columns is equal.
Exercise 1: Use the zeros function to create a matrix with 6 rows and 3 columns that all elements are zero. Assign the result to a variable called x.
Trick To make our code more readable, there is a special style in writing the zeros command input in MATLAB and similar commands. Usually, a space is left after the comma for better readability of the commands. For example, instead of writing zeros (1,3) we write zeros (1, 3).
You may be wondering what is the use of the zero matrix in MATLAB? Suppose we have a very large matrix, for example, 10 by 10. This matrix has 100 elements, right? This matrix has only 5 non-zero elements out of 100 elements and the rest of the elements are zero. In such cases, it is better to first define a zero matrix with dimensions of 10 by 10, then change the non-zero elements. You will learn how to change the amount of data in MATLAB in future sessions. This is one of the applications.
Another use is when we have a variable inside a loop and a number is added to it each time. In this case, first, a zero matrix with definite dimensions is defined and then that matrix is placed inside the ring and filled. This speeds up code execution. You are not yet familiar with the concepts of substitution and loop. So do not worry if you do not understand the use of zeros! In future sessions, you will learn about substitution and the ring. In the final projects of this course, you will practically touch on these applications. So do not miss the next sessions!
Well, the first part of the special matrix session in MATLAB is over. Let’s move on to the next command…
The ones command in MATLAB
The word ones means units. Using the ones command in MATLAB, you can create a matrix whose elements are all the same. So the ones matrix or unit matrix is the second command of special matrices in MATLAB. The inputs of the ones command are similar to the zeros function. That is, the input of the ones statement is also the dimensions of the matrix we want. Similar to zeros, open a parenthesis in front of the ones statement. The first number you enter after parentheses is the number of rows. Then put a comma and enter the second number. The second number indicates the number of columns. For example, to define a 3 in 2 matrix with one element, we need to write:
>> ones(3, 2) ans = 1 1 1 1 1 1
To define a square matrix, like zeros in MATLAB, we can give an input to the ones statement. That is, to define a 5 by 5 matrix with one element, it is enough to write:
>> ones(5) ans = 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
Similar to zeros, the ones (5) and ones (5, 5) commands have similar results (see for yourself). The ones used to produce arrays with three or above that currently do not consider them.
Exercise 2: Create a variable called x, which is a 7-dimensional column vector with one element.
How to create a single matrix in MATLAB was very similar to how to create a zero matrix in MATLAB. The next command is the same. I have given less explanations because it is repetitive…
Eye command in MATLAB
Identity matrix is another special matrix in MATLAB. The eye command in MATLAB is used to define an identity matrix. An identical matrix is a square matrix in which the elements on its principal diameter are one and the other elements are zero. As you know, any matrix multiplied by the same matrix is the result of that matrix itself. That is why it is called the same matrix. The eye command can be used to create such a matrix in MATLAB. For example, to define an identical matrix 3 by 3, we write:
>> eye(3) ans = 1 0 0 0 1 0 0 0 1
Identity matrix is a square matrix. But in MATLAB, with the eye command, you can also define non-square matrices with zero elements on their original diameter. In this case, the output matrix is no longer the same. However, MATLAB can also define such a matrix. For example, if we want to define a 3 in 2 matrix whose elements are zero on its original diameter, we must write:
>> eye(3, 2) ans = 1 0 0 1 0 0
As you can see in the output matrix, the elements on the main diameter (as far as they are concerned) are one and the rest of the elements are zero.
Trick You might be interested to know that the eye command returns a number if it has no input:
>> eye ans = 1
Inf command in MATLAB
In the MATLAB tricks session, we got acquainted with inf. We used inf to define a variable. In this session, we want to create a matrix with inf that all its elements are inf. To do this, just enter it as before. That is, open the brackets in front of inf. First, enter the number of rows and then put a comma. Then enter the number of columns and close the parentheses. Suppose we want to define a 2-by-3 matrix whose all elements are inf. In this case, we must write:
>> inf(2, 3) ans = Inf Inf Inf Inf Inf Inf
To define a square matrix with inf, we must follow the same instructions as before. That is, write only one number in the input. That number is the number of rows or columns of the matrix we want. For example, to define a 2 by 2 square matrix whose elements are all inf, we must write:
>> inf(2) ans = Inf Inf Inf Inf
Nan command in MATLAB
We also got acquainted with the nan command in MATLAB in the MATLAB tricks session. In this session, we want to create a matrix with the nan command, all elements of which are nan. Based on the description of this session, you probably know how to do it. To do this, we do exactly the same as the inf command. To define a 4 by 2 matrix whose elements are all nan, we must write:
>> nan(4, 2) ans = NaN NaN NaN NaN NaN NaN NaN NaN
The nan command, like the previous commands, returns a square matrix if it receives an input. For example, to have a 2-by-2 matrix whose elements are all nan, we must write:
>> nan(2) ans = NaN NaN NaN NaN
But where is the use of nan? In some numerical data, the phrase nan is seen. For example, suppose you have data on the amount of monthly rainfall in Tehran in 1998. But in it, for example, for the month of September, there is the phrase nan. This means that the amount of rainfall in September has not been recorded for any reason. This is a common occurrence. So in the future, wherever you come across data in the form of nan, know that the information in that particular case does not exist or is not recorded. In general, in the construction of numerical data, it is common to first define a nan matrix and then pour the corresponding values into it.
Summary of specific matrix sessions in MATLAB
Here is what we learned in this session in the form of a question and answer session. Answer each question first and then see the answer. Honestly, do not be lazy 😊 😊
Question 1: How to create a zero matrix in MATLAB?
Question 2: What matrix does the ones command produce in MATLAB?
Question 3: What does the eye command do in MATLAB?
Question 4: How to create a matrix or vector with the inf command in MATLAB?
Question 5: How to build a NaN matrix in MATLAB?