Experiment No. 3 Working With Matrices: 2-1 Entering Matrix
Experiment No. 3 Working With Matrices: 2-1 Entering Matrix
______________________________________________________________________________________________
Experiment No. 3
Working with Matrices
2-1 Entering Matrix
The best way to get started with MATLAB is to learn how to handle matrices. The basic
conventions to be followed are:
For example
» A = [16 3 2 13; 5 10 11 8; 9 6 7 12; 4 15 14 1]
Once you have entered the matrix, it is automatically remembered in the MATLAB
workspace. You can refer to it simply as A. Also you can enter and change the values of
matrix elements by using workspace window.
(3- 1)
University of Zakho Petroleum Engineering Department MATLAB Laboratory
______________________________________________________________________________________________
2-2 Subscripts
You can do the above summation, in simple way by using sum command.
If you try to use the value of an element outside of the matrix, the following error
massage would be appeared.
» t = A(4,5)
??? Index exceeds matrix dimensions.
On the other hand, if you store a value in an element outside of the matrix, the size
increases to accommodate the newcomer. The initial values of other new elements are
zeros.
» X = A;
» X (4, 5 ) = 17
A =
16 3 2 13 0
5 10 11 8 0
9 6 7 12 0
4 15 14 1 17
(3- 2)
University of Zakho Petroleum Engineering Department MATLAB Laboratory
______________________________________________________________________________________________
100 93 86 79 72 65 58 51
The colon by itself refers to all the elements in a row or column of a matrix and the
keyword end refers to the last row or column. So
(3- 3)
University of Zakho Petroleum Engineering Department MATLAB Laboratory
______________________________________________________________________________________________
mean (x) The average of the elements of x. For matrices,
Examples mean(x) is a row vector with the average over each
» x = [1 2 3 column.
4 5 6];
» mean(x)
ans =
2.5 3.5 4.5
Single quote ( ' ) Matrix transpose. It flips a matrix about its main
Examples diagonal and it turns a row vector into a column
» x = [1 2 3 vector.
4 5 6
7 8 9];
» x'
ans =
1 4 7
2 5 8
3 6 9
» v = [1 2 3];
» v'
ans =
1
2
3
(3- 5)
University of Zakho Petroleum Engineering Department MATLAB Laboratory
______________________________________________________________________________________________
» min(min(x))
ans =
1
Inv (x) 1
Produce the inverse of matrix x, i.e .
examples x
» x = [1 4
5 8];
» inv(x)
ans =
-0.6667 0.3333
0.4167 -0.0833
Note:
When we are taken away from the world of linear algebra, matrices become two-
dimensional numeric arrays. Arithmetic operations on arrays are done element-by-
element. This means that addition and subtraction are the same for arrays and matrices,
but that multiplicative operations are different. MATLAB uses a dot( . ) or decimal point,
as part of the notation for multiplicative array operations.
(3- 7)
University of Zakho Petroleum Engineering Department MATLAB Laboratory
______________________________________________________________________________________________
Example: Find the factorial of 5
» x = 2:5;
» prod (x)
Exercises:
1- If x = [2 1; 5 3], find:
a) The inverse matrix of x.
b) The diagonal of x.
c) The transpose of x.
(3- 8)