If you ever tried to work with huge matrices, you will know how unpleasant and tedious. Here is where Matlab come to play, it makes working with Matrices easier.
With Matlab, one of the major problem for beginners is to understand how the software works and what the software need in order to help them accomplish their goal using it.
In this read, we will hand over to you some basic Matlab Matrix operation and how to use them to get what you want.
Write a Matrix in Matlab
Find the size of a Matrix
Divide Matrices element by element
Find the inverse of a Matrix
Find the determinant of a Matrix
Define a Matrix with Random elements
Find the diagonal of a Matrix
Compute the Transpose of a Matrix
Extract an element in a Matrix
Multiply 2 Matrices element by element
Create a Matrix with all elements equal to zero
Create a Matrix with All elements equal to one
Matlab Matrix Operations
we will write
A=[1 1 -2;2 2 1;2 1 1]
after pressing ENTER, here is how it will look in Matlab window
The size of a Matrix is its number of rows and columns. To find the size of a Matrix, use the following code
Note A here is the matrix we created in the previous step.
Here is its size
Meaning, A has 3 rows and 3 columns.
Let’s try a second example.
If we type
We will see the following
To add two matrices A and B, we need size(A) to be identical to size(B)
So, let’s create a new Matrix C with the same size as A
Now we can add A and C using the following code
To divide two Matrices element by element use the following
Remember both matrices need to have the same size.
To find the inverse of a Matrix, use the code:
To find the determinant of a Matrix in Matlab, use the following code
To create a Matrix with Random element in Matlab, use
Where (3,2) is the size of the Matrix
DIAG help access diagonals of Matrices in Matlab.
To find the main diagonal of A, we will use
to find the first upper diagonal use
to find the first lower diagonal use
Here is how Matlab read matrix diagonals
To find the transpose of a Matrix, use the following
Here is the transpose of A
You can individually access element of a Matrix or a whole vector.
Let’s consider the following Matrix
If I need to access the first row of the Matrix, I will use the following code
I will use the following to access the element on the first rows-second column.
The following will help access element of the third column
To multiply A X B, A and B being two distinct matrices, A and B has to obey these conditions.
To multiply A by B in Matlab, use the code
To multiply a Matrices element by element, remember the size of the two matrices has to be the same.
Use the following line
To create a Matrix with all elements equal to zero, use the following code
where (3,4) is the size of the Matrix
To create a Matrix with all elements equal to one, use the following code
where (4,5) is the size of the Matrix