Welcome back to MATLAB TUTORIAL- 2 ( Lecture- 4 )
In last tutorial we introduced matlab interface and learned very basic assigning operation and where to use semicolon in matlab.
So, this tutorial will be a bit more advance from the introduction tutorial as we will cover some basic algebraic concepts of matrices in MATLAB.
Lets get started :
As by now we have learned how to assign values to matrices in matlab in our last tutorial ( here is the link for previous tutorial)
Matrix Addition in Matlab :
Insert two Matrices A and B having values [ 1 , 2 ; 3 , 4 ] and [ 3 , 4 ; 5 , 6 ]
After inserting these two matrices just type in: A+ B
without using semicolon or otherwise we would end up with no result.
Alternatively, instead of typing just : A + B
We can also write these commands which seems more formal:
>> C= A+B;
>> C
It will look something like as follows :
Matrix Subtraction in Matlab :
We already have two matrices inserted A and B, rather than re-writing the values we would be using the same matrices for subtraction as well.
Just type in: A- B (If you want to subtract B from A)
without using semicolon or otherwise we would end up with no result.
Alternatively, instead of typing just : A - B
We can also write these commands which seems more formal:
>> D= A-B;
>> D
It will look something like as follows :
Matrix multiplication in Matlab :
If you have no idea about matrix multiplication please go through this tutorial for better understanding as simple multiplication and matrix multiplication are very different.
We already have two matrices inserted A and B, rather than re-writing the values we would be using the same matrices for subtraction as well.
Just type in: A*B ( A*B is different from B*A)
without using semicolon or otherwise we would end up with no result.
To do so we use command:
>> A./B
Accessing elements in a Matrix :
Before starting how to do so lets insert [4x4] matrix F in MATLAB
>>F=[1,2,3,4;5,6,7,8;4,3,2,1;8,7,6,5]
It would look something like this :
Now, suppose we want to access element "7" from 2nd row and 3rd column,
To do so we will write >> F(2,3)
And we can access any element like this by just following syntax:
[Matrix name]([row number],[column number]) and press enter.
Now, suppose we wish to access a complete row(suppose 2nd row) than our command would be :
>>F(2,:)
And to access full column (lets say 3rd column) we command:
>>F(:,3)
Now suppose we want to access following element block [2x2] from this complete matrix [4x4]:
We would use >> F(2:3,3:4)
Which means elements residing from 2nd and 3rd row and corresponding 3rd and 4th column.
which should look like this:
In last tutorial we introduced matlab interface and learned very basic assigning operation and where to use semicolon in matlab.
So, this tutorial will be a bit more advance from the introduction tutorial as we will cover some basic algebraic concepts of matrices in MATLAB.
Lets get started :
As by now we have learned how to assign values to matrices in matlab in our last tutorial ( here is the link for previous tutorial)
Matrix Addition in Matlab :
Insert two Matrices A and B having values [ 1 , 2 ; 3 , 4 ] and [ 3 , 4 ; 5 , 6 ]
After inserting these two matrices just type in: A+ B
without using semicolon or otherwise we would end up with no result.
Alternatively, instead of typing just : A + B
We can also write these commands which seems more formal:
>> C= A+B;
>> C
It will look something like as follows :
Matrix Subtraction in Matlab :
We already have two matrices inserted A and B, rather than re-writing the values we would be using the same matrices for subtraction as well.
Just type in: A- B (If you want to subtract B from A)
without using semicolon or otherwise we would end up with no result.
Alternatively, instead of typing just : A - B
We can also write these commands which seems more formal:
>> D= A-B;
>> D
It will look something like as follows :
Matrix multiplication in Matlab :
If you have no idea about matrix multiplication please go through this tutorial for better understanding as simple multiplication and matrix multiplication are very different.
We already have two matrices inserted A and B, rather than re-writing the values we would be using the same matrices for subtraction as well.
Just type in: A*B ( A*B is different from B*A)
without using semicolon or otherwise we would end up with no result.
Simple multiplication of corresponding elements of other matrix in Matlab :
If we simply want to multiply first element of matrix A with first element of matrix B , and second element of matrix A with second element of matrix B and so on. We can even do this in Matlab by using command:
>>A.*B
And it looks as follows:
There is no as such matrix division but we have element to element division ( first element of matrix A with first element of matrix B , and second element of matrix A with second element of matrix B and so on )
To do so we use command:
>> A./B
Accessing elements in a Matrix :
Before starting how to do so lets insert [4x4] matrix F in MATLAB
>>F=[1,2,3,4;5,6,7,8;4,3,2,1;8,7,6,5]
It would look something like this :
Now, suppose we want to access element "7" from 2nd row and 3rd column,
To do so we will write >> F(2,3)
And we can access any element like this by just following syntax:
[Matrix name]([row number],[column number]) and press enter.
Now, suppose we wish to access a complete row(suppose 2nd row) than our command would be :
>>F(2,:)
And to access full column (lets say 3rd column) we command:
>>F(:,3)
Now suppose we want to access following element block [2x2] from this complete matrix [4x4]:
We would use >> F(2:3,3:4)
Which means elements residing from 2nd and 3rd row and corresponding 3rd and 4th column.
which should look like this:
You, could practice these things out of your free time, this is enough for this tutorial.
Bye Bye.
Comments
Post a Comment