> Financial Mathematics |
Matrix in Matlab
Creating Matrix (or two dimensional array) of different sizes in Matlab. How to show a particular entry of a matrix. Transpose, size and inverse of a matrix.
Q 1. Create the following one row matrix
>> A = [3 4 8] (press Enter)
ans =
3 4 8
Q 2. Create the following two row matrix
>> A = [1 2 3; 4 5 6] (press Enter)
ans =
1 2 3
4 5 6
Q 3. Create the following three row matrix
>> A = [1 2 3; 4 5 6; 7 8 9] (press Enter)
ans =
1 2 3
4 5 6
7 8 9
The general form of a 3x3 matrix (matrix with 3 rows and 3 columns) is
a11 means element at row-1 and column-1
a12 means element at row-1 and column-2
a32 means element at row-3 and column-2
and son on...
Now, consider the above 3x3 matrix
>> A (1,1) (press Enter)
ans =
1
It shows the element in the row-1 and column-1
>> A (2,3) (press Enter)
ans =
6
It shows the element in the row-2 and column-3
>> A (3,2) (press Enter)
ans =
8
It shows the element in the row-3 and column-2
>> A (1,:) (press Enter)
ans =
1 2 3
It shows the elements in the row-1
>> A (3,:) (press Enter)
ans =
7 8 9
It shows the elements in the row-3
>> A (:,2) (press Enter)
ans =
2
5
8
It shows the elements in the column-2
>> A (:,3) (press Enter)
ans =
3
6
9
It shows the elements in the column-3
>> A (2,1:2) (press Enter) ans = 4 5 |
It shows the elements in the row-2 from 1 to 2
>> A (3,2:3) (press Enter)
ans =
8 9
It shows the elements in the row-3 from 2 to 3
>> A (1:2,2) (press Enter)
ans =
2 5
It shows the elements in the column-2 from 1 to 2
>> A (2:3,1) (press Enter)
ans =
4 7
It shows the elements in the column-1 from 2 to 3
Size of a Matrix
>> size (A) (press Enter)
ans =
3 3
It shows size of the matrix A which is 3 3 (3 rows and 3 columns)
Diagonal of a Matrix
>> diag (A) (press Enter)
ans =
1
5
9
It shows elements in the diagonal of matrix A.
Transpose of a Matrix
>> A' (press Enter)
ans =
1 4 7
2 5 8
3 6 9
It converts row-1 into column-1, row-2 into column-2 and so on..
Inverse of a Matrix
Using Matlab find inverse fo the matrix.
>> A = [1 2 9; 0 3 6; 2 5 1];
>> B = inv (A) (press Enter)
B =
0.4737 -0.7544 0.2632
-0.2105 0.2982 0.1053
0.1053 0.0175 -0.0526
It shows inverse of the matrix A.
Now show that A*B = I (Where I is identity matrix).
>> A*B (press Enter)
ans =
1 0 0
0 1 0
0 0 1
It shows that (matrix)*(inverse of matrix) = Identity matrix.