Hello, learners welcome to The Engineering Projects. We are working on MATLAB, and in this tutorial, you are going to learn a lot about matrices in MATLAB. We are going to learn them from scratch, but we will avoid unnecessary details about the topic. So, without wasting time, have a look at the topics that you will learn in detail.

  • What is an array?

  • What is the matrix?

  • How can we declare a matrix in MATLAB?

  • What are the different types of matrices?

  • Can we find the unknown values of two equal matrices?

  • How can we solve the simultaneous equation in MATLAB?

What is an Array?

In this world of technology, the use of data is everywhere, and therefore, we can say there is a need for arrays in every field. You will find the reason soon. But before this, look at the introduction of an array.

 An array is a simple data structure that contains a collection of data presented in contiguous memory locations.

So, the term “contiguous” used in the definition tells us that the data is in a continuous format, so we are not required to search here and there because the data is in a structured format. Moreover, arrays are of many kinds, such as

  • Two-dimensional arrays

  • Three-dimensional arrays

In different types of cases, the suitable array is picked up so that we may get the best result with limited memory occupancy. With this type of foundation concept, we can now move forward toward our main topic, which is matrices. 

What is a Matrix?

In real-life applications and in higher studies, matrices are used in plenty in different forms, and therefore, we have decided to talk about them from a very basic level since it is important to understand the key features of the topics we are studying. Moreover, matrices are introduced in early classes, and it is important to refresh the basics in our minds so that we may proceed to the more complex problems. Here is the definition of "matrix":

A matrix is a two-dimensional array in the form of an ordered rectangular array enclosed by a square bracket that has entries of the same kind in it in the form of real or complex data.

The plural of the matrix is matrices, and sometimes the rectangular bracket is replaced by the parentheses according to the case. Just look at the image given below:

matrix matlab, matlab matrix, matrices in matlab, matlab matrices, what is matrix
Simple 3x3 Matrix

This is a matric that contains nine elements, and you can also name this matric anything you want. In this way, it becomes easy to deal with more than one matrix, and you will see this action soon.

Order of a Matrix

To proceed forward, you must know the types of matrix and, for this, it is important to know the order of the matrix.

matrix matlab, matlab matrix, matrices in matlab, matlab matrices, what is matrix
Order of a Matrix

The matrix given above is a square matrix and the horizontal lines are called columns, whereas the vertical entries are termed the rows of that particular matrix.

If we represent the rows with the name m and the columns as n, then the order of the matrix is given as:

mxn

In this way, it is clear that the matrices given above have the order 3x4. If it seems to be an unnecessary thing to you, think again, because, with the help of order, we can know the type of a matrix and then perform different types of operations on it. But before this, have a look at some code in MATLAB to design matrices of different kinds. 

Code for the Simple Matrix

The Matrix is easily used in MATLAB, and you can start working with it by following the simple steps given below:

  • Start your MATLAB software.

  • Go to the command window.

  • Start writing the following code:

A=[23 14 -8 33; 17 -102 0 37;3 -31 98 4];

  • Press enter. 

In the image given overhead, these are the same entries that we have seen in the image given above, and in MATLAB, you will see the following result:

matrix matlab, matlab matrix, matrices in matlab, matlab matrices, what is matrix
Matrix in MATLAB

The square bracket is not shown on the sides of the array in MATLAB. As you can see, the semicolon after every three entries indicates that the row is completed and the MATLAB compiler has to start the other row. 

Here, A shows the name of the matrix that is compulsory, and you can name your matrix any word. If you do not follow the exact format and provide the number of entries different in rows, you will get the error. Once you know how to get started, you are ready to learn about the types of matrices.

Types of Matrices

There are several different types of matrices, and you can perform different arithmetic operations on the matrices only if they are of the same kind. This condition is not applied to all the operations, but most of them follow these rules. Here are some important types of matrices.

Row Matrix

A row matrix contains only one row and it is one of the simplest forms of a matrix. In this way, we get the matrix with a horizontal shape. The order of this matrix is:

mxn=1xn

Where n may be any number. 

Column Matrix

As you can guess, the column matrix is a type of matrix containing only one column and one or multiple rows. In this way, we get a matrix that has a vertical shape. Have a look at the order of a column matrix:

mxn=mx1

Where m may be any number, but the value of n is always one.

Square Matrix

A square matrix always has the number of rows and columns equal. It means, that no matter what the total number of entries is, the number of entries in each row and column must always be equal. In other words,

m=n

When you examine the example of a square matrix, you will get the reason why it is called so. The shape of this type of matrix is always square.

Rectangular Matrix

A rectangular matrix is one that has the arrangement of elements in such a way that the number of rows of the matrix is not equal to the number of columns. The same statement can be represented in the equation given next:

m!=n

Therefore, the matrix formed is in a rectangular shape, either in vertical format or horizontal format, according to the number of rows and columns.

Diagonal Matrix

We all know that the diagonal is the line or area that joins the upper left area with the lower right area of a rectangular or square. By the same token, a diagonal matrix is the one that contains all the diagonal values equal to zero and s in such a way that all the values other than the diagonal are zero. It will be clearer when you see the example of the diagonal matrix. We have set the examples of all the types of matrices that we have defined previously into a single MATLAB screen so you may have the best idea of each of them.

Code and Output

matrix matlab, matlab matrix, matrices in matlab, matlab matrices, what is matrix

Moreover, here you can observe that instead of naming the matrices A, B, and so on, we have used the real names of the matrices for a clear declaration. Your homework is to make examples of each of them by yourself for the sake of practicing.

Finding the Unknown Values Between Two Matrices

Do you remember when we said the order of the matrix matters? This is one of the uses of an order of a matrix. Suppose we have two matrices named A and B, declaring that both are equal. This means that each corresponding value of a matrix A at position row 1 column 1 is equal to the corresponding value of the same position of matrix B. This is true for all the remaining values q of both matrices. Let me be clear with one example. Have a look at the picture given below:

matrix matlab, matlab matrix, matrices in matlab, matlab matrices, what is matrix

So, the value of r and, in return, the value of all r variables in each entry can be easily obtained by following the rules of the equation. It is one of the simplest examples of doing so, but in real life, we face complex problems. So, we use MATLAB for simplicity and accurate results. Have a look at the MATLAB code where we are going to show you an application of you can easily solve the simultaneous equation in MATLAB as well. 

Solving Simultaneous Equations in MATLAB

By using the property of the matrix of equality in more than one matrix, we can easily solve the simultaneous equations that are difficult and time taking if we solve them by hand. So let's see how we can declare and solve the simultaneous equation in MATLAB.

Code:

syms x y

equa1= 6*x + 9*y==13;

equa2= 9*x + 6*y==12;

[A,B]= equationsToMatrix([equa1,equa2],[x,y])

z=linsolve(A,B)

Output:

matrix matlab, matlab matrix, matrices in matlab, matlab matrices, what is matrix

Understanding the Code

To understand this code, you have to learn the basic definition of the function we have used in the code. It is the equationsToMatrix function. 

equationsToMatrix Function

The equationsToMatrix is a pre-defined function of MATLAB that converts the linear equation into a matrix so that we can use different operations on it more efficiently. It does it in the same way as we do in real life while solving the simultaneous equation with pen and paper. There are three types of syntax if this particular function. The one that we have used has the following syntax:

[A,b] = equationsToMatrix(eqns,vars)

Here, a minimum of two equations are required and the variables have the same condition. You must keep all the functions in mind and have to follow the exact syntax. Otherwise, it will show an error.

linsolve Function in MATLAB

In MATLAB, to solve the linear equation, we use this pre-defined function as it works in two ways:

  1. LU factorization with partial pivoting when in equation AB=X, A is a square. 

  2. QR factorization, otherwise.

In our case, it has used the QR factorization. Now, you are able to understand the code clearly. 

  • First of all, the syms sign tells MATLAB that we are defining the variables. These may be one or more. But, we wanted two variables here, and we named them x and y. 

  • Now, we simply provide the values of the equation to MATLAB and store both of them into variables named equa1 and equa2 respectively. 

  • The values of variables and equations are fed into the eqautionToMatrix function to convert the linear simultaneous equation into a matrix for easy solving. 

  • In the end, we simply named a matrix z and told MATLAB that we wanted the value of variables x and y.

Simultaneous Equation in MATLAB: Method 2

By the same token, we can use the other method that is similar to it but the way it solves the equation is a little bit different. 

Code:

syms x y

equa1= 6*x + 9*y==13;

equa2= 9*x + 6*y==12;sol=solve([equa1,equa2],[x,y])

asol=sol.x

 bsol=sol.y

Output:

matrix matlab, matlab matrix, matrices in matlab, matlab matrices, what is matrix

Here, the only pone this is to understand. sol.x and sol.y are the functions that are used by the compiler to find the value of variables x and y respectively. You can use any variable with this sol function, after naming them at the beginning. After that, a variable is used to store and present the value of the answer obtained.

It was an interesting lecture about the matrix, and we worked a lot from scratch to the end on many topics. We have defined the arrays and seen the introduction of the matrix. We also found information about the types of matrices. Once we have a grip on the basics, we learn that a matrix can be used to find the unknown value of two matrices, and as an application of this method, we found the values of the variable by using linear equations and learned how to declare, use, and solve the linear equation with the help of matrices in MATLAB.