0% found this document useful (0 votes)
3 views7 pages

Experiment 2

The document provides an introduction to matrices and arrays in MATLAB, detailing how to enter, index, and manipulate matrices. It explains matrix operations such as deleting rows and columns, using the colon operator for sub-matrix extraction, and concatenation of matrices. Additionally, it includes practical exercises to reinforce the concepts presented.

Uploaded by

rasha waleed
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)
3 views7 pages

Experiment 2

The document provides an introduction to matrices and arrays in MATLAB, detailing how to enter, index, and manipulate matrices. It explains matrix operations such as deleting rows and columns, using the colon operator for sub-matrix extraction, and concatenation of matrices. Additionally, it includes practical exercises to reinforce the concepts presented.

Uploaded by

rasha waleed
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/ 7

College of Electronics

Eng . Electronic Eng. Dep.


2 nd Year
MATLAB LAB
Lecture (2): Matrices & Arrays
1-Introduction:
In MATLAB, a matrix is a rectangular array of numbers. Special meaning is sometimes attached
to 1-by-1 matrices, which are scalars, and to matrices with only one row or column, which are
vectors. MATLAB has other ways of storing both numeric and nonnumeric data, but in the
beginning, it is usually best to think of everything as a matrix. The operations in MATLAB are
designed to be as natural as possible. Where other programming languages work with numbers
one at a time, MATLAB allows you to work with entire matrices quickly and easily.
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- Entering a matrix
A matrix is an array of numbers. To type a matrix into MATLAB you must
 begin with a square bracket, [.
 separate elements in a row with spaces or commas (,).
 use a semicolon (;) to separate rows.
 end the matrix with another square bracket,].
Here is a typical example. To enter a matrix A, such as, simply type in the Command
Window 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 7 6 9
4 15 14 1
This matrix matches the numbers in the engraving. 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. We write,
>> A(2,1) or A(2)
ans =
5
A(2,1) is an element located in the second row and first column. Its value is 5.

1
College of Electronics Eng .
Electronic Eng. Dep.
2 nd Year
MATLAB LAB
3-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). Thus, A(i,j)
in MATLAB refers to the element Aij of matrix A. The ¯rst index is the row number and
the second index is the column number. For example, A(1,3) is an element of first row and third
column. Here, A(3,3)=6.
Correcting any entry is easy through indexing. Here we substitute A(3,3)=6 by
A(3,3)=0. The result is
>> A(3,3) = 0
A=
16 3 2 13
5 10 11 8
9 7 0 9
4 15 14 1
Single elements of a matrix are accessed as A(i,j), where i 1 and j 1. Zero or negative
subscripts are not supported in MATLAB.

4- Deleting Rows and Columns


You can delete rows and columns from a matrix using just a pair of square brackets. Start with
X = A;
Then, to delete the second column of X, use
X(:,2) = [ ]
This changes X to
X=
16 2 13
5 11 8
9 7 12
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

2
College of Electronics Eng .
Electronic Eng. Dep.
2 nd Year
MATLAB LAB
5-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 column k to l. Subscript expressions refer to
portions of a matrix. For example,
>> A(2,:)
ans =
5 10 11 8
is the second row elements of A.
The colon operator can also be used to extract a sub-matrix from a matrix A.
>> A(:,2:3)
ans =
3 2
10 11
7 0
15 14
A(:,2:3) is a sub-matrix with the two and three columns of A.
thus
** A(:,j) is the jth column of A, while
** A(i,:) is the ith row, and
**A(end,:) picks out the last row of A.
The keyword end, used in A(end,:), denotes the last index in the specifed dimension. Here are
some examples.
>> A
A=
1 2 3
4 5 6
7 8 9
>> A(2:3,2:3)
ans =
>> A(end:-1:1,end)
ans =
>> A([1 3],[2 3])
ans =

3
College of Electronics Eng .
Electronic Eng. Dep.
2 nd Year
MATLAB LAB
6- 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 an example, start with the 4-by-4 magic square, A, and form
X=
16 2 13
5 11 8
9 7 12
4 14 1
B = [X X -3]
B=
16 2 13 13 -1 10
5 11 8 2 8 5
9 7 12 6 4 9
4 14 1 1 11 -2
This matrix is halfway to being another magic square. Its elements are a rearrangement of the
integers 1:64. Its column sums are the correct value for an 8-by-8 magic square.

Experiments:
Q1- Given the following matrix
x=
16 3 2 13
5 4 8 7
Write the result of the following commands.
1- >> x(:,2)

2- >> x(2,3)

3- >> x(2,:)

4- >> x(1,end)

4
College of Electronics Eng .
Electronic Eng. Dep.
2 nd Year
MATLAB LAB
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- You have this MATRIX.


D=
9 8 7 9
6 5 4 6
3 2 1 3
8 5 2 3
Write Command That Do The Following Action.
1-
D=
9 0 7 0
6 5 4 6
3 2 1 3
8 5 2 3

5
College of Electronics Eng .
Electronic Eng. Dep.
2 nd Year
MATLAB LAB
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

6
College of Electronics Eng .
Electronic Eng. Dep.
2 nd Year
MATLAB LAB
Q3given the following array
A=[1 2 5 3;1 5 3 0;0 1 5 2;0 6 4 7]
Find the result after concatenate it as bellow without use matlab.
A=[a-4 ;a+3 ]
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