How to generate matrix and vectors in Matlab, Example codes
Table of Contents
Matlab Matrix and Matlab Vectors:
matrix and vectors in Matlab- In this article, we will discuss how to generate matrices and vectors in MATLAB and how to manipulate the data as MATLAB treats each and every computation in terms of vectors and matrices. Therefore it is necessary to understand how to generate matrices and vectors and how to manipulate the data available in those matrices and vectors.
Generation of matrix:
First of all let me show you how to generate matrices having specific dimension in MATLAB. So let’s say i want to generate a matrix having dimensions 3X3 means 3 rows and 3 columns.
A= [1 2 3; 4 5 6; 7 8 9]
So I will write here A is equal to these square brackets and first of all i will write the contents of the first row and let’s say those contents are 1, 2, 3 then write semicolon operator which will starts a new row. The content of the second row are 4, 5 and 6 and the third row is 7, 8 and 9.
When we put semicolon “ ; ” it will start a new row and use space between the content to show separate column.
A= [1 2 3; 4 5 6; 7 8 9]
So this is 3X3 matrix named as “A”.
Extraction of Row and Column from Matrix:
Now i will tell you how to extract several rows and columns and values out of this matrix “A” first of all let’s say i want to extract only the first row of this matrix “A” and assign it to another matrix “B”. So i will write
B = A (1,:)
“B” is equal to A (,) these parentheses are used to index the matrix “A” and the comma separates the rows and columns. So the entry before comma is the row index and entry after the comma is the column index. Now because i want to extract the first row so i will write here 1 and after comma i will write colon which shows that the first row contains the elements from all the available columns. So running this command i will extract the first row of the matrix “A”.
B = A (1,:)
Similarly, i can extract the column of the matrix “A” so i will write here
“B” is equal to “A” the same parenthesis and the comma operator now before the comma i will write colon and after comma i will write 1 which shows that i want to extract the first column of the matrix a so it is the first column
Similarly, i can also extract more than one columns and rows of the matrix “A” the method is i will write
“B” is equal to a parenthesis and i will write here the range of the values for the row. So if i want to extract the first two rows i will write one colon 2 and then comma separator and after comma i will write again a column operator showing that I want to use all the columns so i have successfully extracted the first two rows of the matrix “A”.
Similarly, i can extract more than one columns as well to do this i will write
“B” is equal to “A” parenthesis and here i will write colon comma and let’s say i want to extract the second third column so i will write 2 colon 3. So this will extract the last two columns which are column number 2 and column number 3.
Similarly, i can also extract a single value of the matrix for example if i want to extract the value 4 then how to do this first of all i will have to see which row and which column is used to specify this single value so we can see that this entry 4 is available or present in the second row and first column. So i will write here
B is equal to a second-row comma first column. So this will generate the value 4 and assign it to the variable B.
Now, i will tell you how to replace the values inside a matrix let’s say i want to replace the first row of matrix “A” with zeros. So to do this i will write here
first row of “A” by using the same method “A” 1 comma colon and on the right side i will write zeros 1 comma 3 this is the built-in command which generates three zeros in the form of a row vector. So when running this command you can see that the first row of a is replaced with all zeros.
By using a similar procedure you can always replace a single value also for example i want to replace this value 7 with let’s say minus 1. So how to do that First of all, I will write the indices of this value 7 it lies in the third row and first column. So i will write here
3 comma 1 and on the right side i will insert the value which i want to replace which is minus 1 in our case. So by doing this this value 7 is replaced with -1. So this is how you can replace single value or the complete row or complete column and even more than one columns and more than one rows.
Operation on Matrices in Matlab:
Now i will tell you some fundamental concepts about the operations that you can perform in matrices.
Addition of matrices in Matlab:
If I want to add two matrices it must be in the same dimension means it has same rows and columns. For example I want to add matric A with matrix B having two rows and columns then I will write the command.
A=[1 2;3 4] B=[4 5;6 7]
We can also add More than matrices for example we have matric c=[9 10;11 12] and we want to add it with A and B than we will write the command
Subtraction of Matrices:
Similar process will be used for the subtraction of matrices for example If I want to subtract two matrices it must be in the same dimension means it has same rows and columns. For example I want to subtract matrix A from matrix B having two rows and columns then I will write the command.
A=[1 2;3 4] B=[4 5;6 7]
Multiplication of Matrices:
Specifically i will tell you here about the multiplication you can keep in mind that MATLAB provides two methods for matrix multiplication. The first one is conventional matrix multiplication and the second one is element wise matrix multiplication.
To perform conventional matrix multiplication it is a necessary condition that the number of columns of the first matrix must be equal to the number of rows of the second matrix otherwise you cannot perform conventional matrix multiplication and in order to perform element wise multiplication the dimensions of both the matrices must be the same. So let’s consider the first conventional method in which we have:
A=[1 2 3;4 5 6;7 8 9 ] B=[10 11 12;13 14 15;16 17 18]
Because the condition is fulfilled in our case both the matrices are 3X3 so it will be a multiplied by b
If you want to perform element wise multiplication i will write here d is equal to A dot multiplied by B so this dot operator is used to specify the element wise multiplication in MATLAB.
So this is the resultant you can see that both the results are pretty much different because entirely the computation is different.
Transpose of matrices:
Now finally i will tell you something about the transpose of matrix so if the matrix in MATLAB is real then transpose and conjugate transpose is the same. But if the contents of the matrix are complex values then the transpose and conjugate transpose become two different things. For example we generate a complex valued matrix over here a 2X2 matrix for example:
A = [1+2j 2+3j; 3+4j 4+5j]
So this is a complex value 2X2 matrix A, now if we want to take simple transpose without taking conjugate then i will write “B” is equal to A dot ‘
This is the command for taking the transpose if i use dot transpose then it will take only the transpose.
So you can see that it has only taken the transpose. The first row became the first column and the second row became the second column.
Conjugate of matrix:
But if you want to take conjugate transpose then i will remove this dot operator and rerun the command to see that here it has also taken the conjugate of the matrix A and also taken the transpose of the matrix both the operations can be achieved by using the single command. So, this is how you can generate matrices and vectors in MATLAB, you can extract different rows and columns and values and also how can you multiply.