0% found this document useful (0 votes)
6 views

Lecture 3

The document describes various operations that can be performed on matrices in MATLAB. It defines what a matrix is and explains that it can contain real or imaginary numbers. It also defines vectors and scalars as special types of matrices. The document then explains how to specify matrices by listing elements row-wise within square brackets and separating rows with semicolons. It provides examples of extracting, replacing, and extracting sub-matrices. It also describes how to add or delete rows and columns and limitations on deleting multiple elements with one operation.
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)
6 views

Lecture 3

The document describes various operations that can be performed on matrices in MATLAB. It defines what a matrix is and explains that it can contain real or imaginary numbers. It also defines vectors and scalars as special types of matrices. The document then explains how to specify matrices by listing elements row-wise within square brackets and separating rows with semicolons. It provides examples of extracting, replacing, and extracting sub-matrices. It also describes how to add or delete rows and columns and limitations on deleting multiple elements with one operation.
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/ 16

𝑎11 𝑎21 𝑎31

 𝐴 = 𝑎21 𝑎22 𝑎32 is a matrix of dimension 3 ×3


𝑎31 𝑎32 𝑎33

𝑎31 = element of Matrix A, Row = 3, Column =1

 Matrix element can be real no. or imaginary no.


 Matrix having only one row or one column is called vector.
 Matrix having one column of n- elements is called column
vector ( dim: n×1)
 Matrix having one row of n- elements is called row vector
( dim: 1×n)
 Matrix having only one element is called Scalar ( dim: 1×1)
 Thus a variable “A” defined in the Matlab can be either Scalar,
Vector or Matrix
 Entry of all the elements are made inside square
bracket “[]”
 Entry is made row-wise ( i.e. 1st entry of all the
elements of 1st row followed by next row)
 Rows are separated by Semicolon “;”
1 2 3
𝐴= 4 5 6
7 8 9
>> A = [1 2 3; 4 5 6; 7 8 9];
 Another way
>> A = [ 1 2 3;
4 5 6;
7 8 9]
 Extract any element of Matrix
>> C= A(i,j): gives an element of matrix A having
ith row and jth column
Example:
>> A=[1 2 3; 4 5 6; 7 8 9];
>> C=A(2,2)
1 2 3
C= 𝐴= 4 5 6
7 8 9
5
 Replace any element of Matrix
>> A(i,j)=k: this element of the matrix will be replaced
by value “k”
Example:
>> A=[1 2 3; 4 5 6; 7 8 9];
>> A(2,2)=10

A= 1 2 3
𝐴= 4 5 6
7 8 9
1 2 3
4 10 6
7 8 9
 Extract a sub-matrix
>> C = A (:, j): Extract elements of all the rows and jth
column
Example:
>> A=[1 2 3; 4 5 6; 7 8 9];
>> C=A(:,2)

1 2 3
C=
𝐴= 4 5 6
7 8 9
2
5
8
>> C = A (i,:): Extract elements of ith rows and all
columns
Example:
>> A=[1 2 3; 4 5 6; 7 8 9];
>> C=A(3,:)
1 2 3
𝐴= 4 5 6
C=
7 8 9

7 8 9
>> C = A (:, a:b): Extract elements of all the rows and column
between a to b
Example:
>> A=[1 2 3; 4 5 6; 7 8 9];
>> C=A(:,2:3)

1 2 3
C=
𝐴= 4 5 6
7 8 9
2 3
5 6
8 9
>> C = A (m:n, :): Extract elements of rows between m to
n and all columns
Example:
>> A=[1 2 3; 4 5 6; 7 8 9];
>> C=A(1:2,:)
1 2 3
C= 𝐴= 4 5 6
7 8 9
1 2 3
4 5 6
>> C = A(m:n, a:b): Extract elements of rows between m
to n and column between a to b
Example:
>> A=[1 2 3; 4 5 6; 7 8 9];
>> C=A(1:2,2:3)

C= 1 2 3
𝐴= 4 5 6
7 8 9
2 3
5 6
>> C = A([ a b c ], [p q r]): Extract elements of selective rows
& columns
Example:
>> A=[1 2 3 4 5; 6 7 8 9 10;
11 12 13 14 15; 16 17 18 19 20; 21 22 23 24 25];
>> C= A([ 1 3 5],[ 3 5] )

C=
1 2 3 4 5
6 7 8 9 10
3 5 𝐴= 11 12 13 14 15
13 15 16 17 18 19 20
23 25 21 22 23 24 25
 Adding Rows
 A is a matrix of dimension (m×n)
 u is Row vector of dimension (1×n)
 A =[A; u] adds one Row in the matrix A
Example:
>> A=[1 2 3; 4 5 6; 7 8 9]; dim: 3x3
>> u = [ 10 11 12]; dim: 1x3
>> A=[A; u]

A=

1 2 3
4 5 6
7 8 9
10 11 12
 Adding Column
 A is a matrix of dimension (m×n)
 v is Column vector of dimension (m×1)
 A =[A v] adds one Column in the matrix A
Example:
>> A=[1 2 3; 4 5 6; 7 8 9]; dim: 3x3
>> v = [ 10; 11; 12]; dim: 3x1
>> C=[A v]

C=

1 2 3 10
4 5 6 11
7 8 9 12
 Deleting Rows
 Select the row/rows you want to delete
 Equate the selected row/rows with null matrix
Example:
>> A=[1 2 3; 4 5 6; 7 8 9];
>> A(1,:)=[ ]
1 2 3
A= 𝐴= 4 5 6
7 8 9

4 5 6
7 8 9
 Deleting Column
 Select the Column/Columns you want to delete
 Equate the selected Column/Columns with null matrix
Example:
>> A=[1 2 3; 4 5 6; 7 8 9];
>> A(:,2)=[]
1 2 3
A= 𝐴= 4 5 6
7 8 9
1 3
4 6
7 9
 A null assignment can have only one non-colon index.
Example:
>> A=[1 2 3; 4 5 6; 7 8 9];
1 2 3
>> A([ 1 3],1:2 )=[] 𝐴= 4 5 6
ERROR 7 8 9
>> A(1:2,1:2 )=[]
ERROR
>> A(3,3)=[]
ERROR

You might also like