Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

SIGNAL AND SYSTEMS LABORATORY

Dept. : Systems and Control

Experiment [2]
Introduction to MATLAB
1-Introduction:
In MATLAB, a matrix is a rectangular array of numbers. Even a scalar is considered as
a 1-by-1 matrix, and a vector as a matrices with only one row or column. MATLAB has
other ways of storing both numeric and non numeric data, but in the beginning, it is
usually best to think of everything as a vector, matrix, array. The operations in MATLAB
are designed to be as natural as possible.
You can enter matrices into MATLAB in several different ways:
1. Enter an explicit list of elements
3. Load matrices from external data files
3. Generate matrices using built-in functions
4. Create matrices with your own functions in M-files

2- Vector
Consider the following examples:
>>x=[0 0.314 0.942 1.57 3.14]
** using the step to select only some elements from x :
>>x(1:2:5)
ans=
0 0.942 3.14
**using a step of -1 :
>> x(5 : -1 : 3)
ans=
3.14 1.57 0.942
>> x(7)
ans=
??? Index exceeds matrix dimensions.
** using the step to define a matrix :
1
SIGNAL AND SYSTEMS LABORATORY

>>y=1:2: 10
y=1 3 5 7 9

3- Matrix
3-1 Entering a matrix
A matrix is an array of numbers. To type a matrix into MATLAB you must:
 Begin with a sequence bracket [
 Separate the elements of the row with spaces or commas
 Use a semicolon (;) to separate rows
 End the matrix with another square bracket ]
For example, to enter a matrix A, type in the command window:
>>A=[16 3 2 13; 5 10 11 8; 9 6 7 12; 4 15 14 1]
A=
16 3 2 13
5 10 11 8
9 7 6 9
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. Now that you have A in the workspace, we
can then view a particular element in a matrix by specifying its location. For example:
>> A(2,1) or A(2)
ans=
5
A(2,l) or A(2) is an element located in the second row and first column. Its value is 5.

3-2 Matrix indexing


We select elements in a matrix just as we did for vectors, but now we need two indices.
The element of row i and column j of the matrix A is denoted by A(i , j). where the first
index is the row number and the second index is the column number.
Any element in a matrix can be reached by its index as the following examples:
>>A(3,3)=0
A=
16 3 2 13
5 10 11 8
9 7 0 9
4 15 14 1

2
SIGNAL AND SYSTEMS LABORATORY

3-3 Deleting Rows and Columns


You can delete any row or column in a matrix using square brackets, as in the
following example (on the same matrix A)
>>x=A;
now, to delete the second column of x:
>>x(:,2)=[ ]

x=
16 2 13
5 11 8
9 0 9
4 14 1

If you delete a single element from a matrix, the result is not a matrix anymore. So
expressions like:
>> x(1,2)= [ ]
result in an error.

3-4 The colon operator


The colon operator can also be used to pick out a certain row or column. For example,
the statement A(m:n,k:l) specifies rows m to n and columns k to l. So it provides a way to
display or deal with only a part of the matrix. For example:
>>A(2, :)
ans =
5 10 11 8

>>A(: , 2:3)
ans =
3 2
10 11
7 0
15 14

Thus:
** A(:,j) is the jth column of A
** A(i,:) is the ith row of A
** A(end,:) picks out the last row of A

The keyword end, used in A(end,:), denotes the last index in the specified dimension as
shown in the following examples:

3
SIGNAL AND SYSTEMS LABORATORY

>>A=
1 2 3
4 5 6
7 8 9

>>A(2:3,2:3)
ans =
5 6
8 9

>> A(end:-1: l,end)


ans =
9
6
3

>>A([1 3], [2 3])


ans =
2 3
8 9

3-5 Concatenation
Concatenation is the process of joining small matrices to make bigger ones. In fact, you
made your first matrix by concatenating its individual elements. The pair of square
brackets, [ ], is the concatenation operator. For example:

x=
16 2 13
5 11 8
9 0 9
4 14 1

>> B=[x , x-3]


B=
16 2 13 13 -1 10
5 11 8 2 8 5
9 0 9 6 -3 6
4 14 1 1 11 -2

4- Array
Array is similar to matrix but the difference will be display in mathematical operation.

4
SIGNAL AND SYSTEMS LABORATORY

Experiments:

Q1) Given the following matrix:


x=
16 3 2 13
5 4 8 7
Write the result of the following:
1-
>> x(:,2)

2-
>> x(2,3)

3-
>> x(2,:)

4-
>>x(1,end)

5-
>>x(1:2,:)

6-
>>x(1:2,1:2)

7-
>>x(end,1:2)

8-
>>x(2,1:2:3)

9-
>> x(1,2:2:4)

10-
>> x(end,1:3:end)

Q2) If you have the following matrix:


D=
9 8 7 9
6 5 4 6
3 2 1 3
8 5 2 3
5
SIGNAL AND SYSTEMS LABORATORY

Write a command that do each of the following actions:

1- D=
9 0 7 0
6 5 4 6
3 2 1 3
8 5 2 3

2- D=
9 8 0 9
6 5 4 6
3 2 0 3
8 5 2 3

3- D=
9 0 0 0
6 0 0 0
3 0 0 0
8 0 0 0

4- D=
9 8 7
6 5 4
3 2 1

5- D=
0 0 0 9
6 5 4 6
0 0 0 3
8 5 2 3

Q3) Given the following array:


A=[1 2 5 3;l 5 3 0;0 1 5 2;0 6 4 7]

1- Find the result of the following without using MATLAB:


>>A=[A-4 ; A+3 ]

2- What is the command used to obtain the following array?


1 2 5 3 6 7 10 8
1 5 3 0 6 10 8 5
0 1 5 2 5 6 10 7
0 6 4 7 5 11 9 12

You might also like