Professional Documents
Culture Documents
Experiment (2) Introduction To MATLAB
Experiment (2) Introduction To MATLAB
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.
2
SIGNAL AND SYSTEMS LABORATORY
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.
>>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
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
4- Array
Array is similar to matrix but the difference will be display in mathematical operation.
4
SIGNAL AND SYSTEMS LABORATORY
Experiments:
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)
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