0% found this document useful (0 votes)
14 views8 pages

EXP - NO2 Matrices

Uploaded by

roneali098
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views8 pages

EXP - NO2 Matrices

Uploaded by

roneali098
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

University of Zakho Mechanical Engineering Department MATLAB Laboratory

______________________________________________________________________________________________

Experiment No. 2
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:

• Separate the elements of a row with blanks or commas.


• Use a semicolon (;) to indicate the end of each row or to start next row.
• Surround the entire list of elements with square brackets, [ ].

For example
» A = [16 3 2 13; 5 10 11 8; 9 6 7 12; 4 15 14 1]

MATLAB displays the matrix you just entered.


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 Mechanical Engineering Department MATLAB Laboratory
______________________________________________________________________________________________
2-2 Subscripts

The element in row i and column j of A is denoted by A (i,j). For example,


A (4,2) is the number in the fourth row and second column. For the above matrix, A
(4,2) is 15.

So to compute the sum of the elements in the fourth column of A, type


» A(1,4) + A(2,4) + A(3,4) + A(4,4)
ans =
34

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

2-3 Colon Operator

The colon " : " is one of the most important MATLAB operators. It occurs in several
different forms. The expression
» 1:10

is a row vector containing the integers from 1 to 10


1 2 3 4 5 6 7 8 9 10

To obtain nonunit spacing, specify an increment. For example,


» 100:-7:50

(3- 2)
University of Zakho Mechanical Engineering Department MATLAB Laboratory
______________________________________________________________________________________________
100 93 86 79 72 65 58 51

Subscript expressions involving colons refer to portions of a matrix.


»A(1:k,j)

is the first k elements of the jth column of A.

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

» A(4,:) or » A(4,1:end) give the same action


ans =
4 15 14 1
» A(2,end)
ans =
8
========================================================

2-4 Basic Matrix Functions


Command Description

sum (x) The sum of the elements of x. For matrices, sum(x) is


examples a row vector with the sum over each column.
» x = [1 2 3
4 5 6];
» sum(x)
ans =
5 7 9
» sum(x,2) sum (x,dim) sums along the dimension dim.
ans =
6
15
To find the sum of elements that are stored in matrix
» sum(sum(x))
ans = with n dimensions, the sum command must be used n
21 times in cascade form, this is also applicable for max,
min, prod, mean and median commands.

(3- 3)
University of Zakho Mechanical 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

» meam(x,2) mean (x,dim) averages along the dimension dim.


ans =
2
5
» mean(mean(x))
ans =
3.5

zeros (N) Produce N by N matrix of zeros.


zeros (N,M) Produce N by M matrix of zeros.
Example
» zeros (2,3)
ans =
0 0 0
0 0 0

ones (N) Produce N by N matrix of ones.


ones (N,M) Produce N by M matrix of ones.
Example
» ones (2,3)
ans =
1 1 1
1 1 1

size(X) Return the size (dimensions) of matrix X.


Examples
» X = [1 2 3
4 5 6];
» size(X)
ans =
2 3

length(X) Return the length (number of elements) of vector v.


Examples
(3- 4)
University of Zakho Mechanical Engineering Department MATLAB Laboratory
______________________________________________________________________________________________
» v = [1 2 3];
» length(v)
ans =
3

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

Max (x) Find the largest element in a matrix or a vector.


examples
» x = [1 5 7
4 3 6];
» max(x)
ans =
4 5 7
» max(max(x))
ans =
7

Min (x) Find the smallest element in a matrix or a vector.


examples
» x = [1 5 7;
4 3 6];
» min(x)
ans =
1 3 6

(3- 5)
University of Zakho Mechanical Engineering Department MATLAB Laboratory
______________________________________________________________________________________________

» min(min(x))
ans =
1

magic (N) Produce N magic square. This command produces


examples valid magic square for all N>0 except N = 2.
» magic(3)
ans =
8 1 6
3 5 7
4 9 2

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

diag (N) Return the diagonal of matrix x. if x is a vector then


examples this command produce a diagonal matrix with diagonal
» x = [1 2 3 x.
4 5 6
7 8 9];
» diag(x)
ans =
1
5
9
» v = [1 2 3];
» diag(v)
ans =
1 0 0
0 2 0
0 0 3

Prod(x) Product of the elements of x. For matrices, Prod(x) is a


Examples row vector with the product over each column.
» x = [1 2 3
4 5 6];
(3- 6)
University of Zakho Mechanical Engineering Department MATLAB Laboratory
______________________________________________________________________________________________
» prod(x)
ans = 4 10 18
prod(prod(x))
ans =
720

median(x) The median value of the elements of x. For matrices,


examples median (x) is a row vector with the median value for
» x = [4 6 8 each column.
3 9 1
8 2 5];
» median(x)
ans =
4 6 5
» median(x,2) median(x,dim) takes the median along the dimension
ans = dim of x.
6
3
5
» median(median(x))
ans =
5

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 Mechanical Engineering Department MATLAB Laboratory
______________________________________________________________________________________________
Example: Find the factorial of 5
» x = 2:5;
» prod (x)

Example: if x = [1, 5, 7, 9, 13, 20, 6, 7, 8], then


a) Replace the first five elements of vector x with its maximum value.
b) Reshape this vector into a 3 × 3 matrix.
Solution:
a)
» x(1:5) = max(x)
b)
» y(1,:) = x(1:3);
» y(2,:) = x(4:6);
» y(3,:) = x(7:9);
» y

Example: Generate the following row vector b=[1, 2, 3, 4, 5, . . . . . . . . . 9,10], then


transpose it to column vector.
Solution:
» b = 1:10
b =
1 2 3 4 5 6 7 8 9 10
» b = b';

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.

2- If x = [1 1 1; 2 2 2], b = [3 3 3], then:


a) Find the maximum and minimum of x.
b) Add the vector b as a third row to x.

3- If x = [2 6 12; 15 6 3; 10 11 1],then reshape this matrix into row vector.

4- Generate a random matrix.

(3- 8)

You might also like