You are on page 1of 96

Matlab

Dr. Muhammad Yousuf Tufail

Department of Mathematics
NED University of Engineering & Technology.
‡ Yousuf Tufail ¯ Yousuf Tufail
tufail@neduet.edu.pk

Lecture 2

Dr. Muhammad Yousuf Tufail (NEDUET) Matlab Lecture 2 1 / 12


Arrays

Dr. Muhammad Yousuf Tufail (NEDUET) Matlab Lecture 2 2 / 12


Arrays

Height of a family members:

Dr. Muhammad Yousuf Tufail (NEDUET) Matlab Lecture 2 2 / 12


Arrays

Height of a family members:


height 1 = 1.64;
height 2 = 1.42;
height 3 = 1.56;
height 4 = 1.80;

Dr. Muhammad Yousuf Tufail (NEDUET) Matlab Lecture 2 2 / 12


Arrays

Height of a family members:


height 1 = 1.64;
height 2 = 1.42;
height 3 = 1.56;
height 4 = 1.80;
It looks quite inconvenient way of representation.

Dr. Muhammad Yousuf Tufail (NEDUET) Matlab Lecture 2 2 / 12


Arrays

Height of a family members:


height 1 = 1.64;
height 2 = 1.42;
height 3 = 1.56;
height 4 = 1.80;
It looks quite inconvenient way of representation.
Matlab provides a better way to represent such data in the form of one
dimensional array or cell as explained below:

Dr. Muhammad Yousuf Tufail (NEDUET) Matlab Lecture 2 2 / 12


Arrays

Height of a family members:


height 1 = 1.64;
height 2 = 1.42;
height 3 = 1.56;
height 4 = 1.80;
It looks quite inconvenient way of representation.
Matlab provides a better way to represent such data in the form of one
dimensional array or cell as explained below:
fam heights=[1.64,1.42,1.56,1.80];

Dr. Muhammad Yousuf Tufail (NEDUET) Matlab Lecture 2 2 / 12


Arrays

Height of a family members:


height 1 = 1.64;
height 2 = 1.42;
height 3 = 1.56;
height 4 = 1.80;
It looks quite inconvenient way of representation.
Matlab provides a better way to represent such data in the form of one
dimensional array or cell as explained below:
fam heights=[1.64,1.42,1.56,1.80];
Calling entries from the cell

Dr. Muhammad Yousuf Tufail (NEDUET) Matlab Lecture 2 2 / 12


Arrays

Height of a family members:


height 1 = 1.64;
height 2 = 1.42;
height 3 = 1.56;
height 4 = 1.80;
It looks quite inconvenient way of representation.
Matlab provides a better way to represent such data in the form of one
dimensional array or cell as explained below:
fam heights=[1.64,1.42,1.56,1.80];
Calling entries from the cell
fam heights(3)

Dr. Muhammad Yousuf Tufail (NEDUET) Matlab Lecture 2 2 / 12


Arrays

Height of a family members:


height 1 = 1.64;
height 2 = 1.42;
height 3 = 1.56;
height 4 = 1.80;
It looks quite inconvenient way of representation.
Matlab provides a better way to represent such data in the form of one
dimensional array or cell as explained below:
fam heights=[1.64,1.42,1.56,1.80];
Calling entries from the cell
fam heights(3)
1.56

Dr. Muhammad Yousuf Tufail (NEDUET) Matlab Lecture 2 2 / 12


Arrays

Height of a family members:


height 1 = 1.64;
height 2 = 1.42;
height 3 = 1.56;
height 4 = 1.80;
It looks quite inconvenient way of representation.
Matlab provides a better way to represent such data in the form of one
dimensional array or cell as explained below:
fam heights=[1.64,1.42,1.56,1.80];
Calling entries from the cell
fam heights(3)
1.56

Dr. Muhammad Yousuf Tufail (NEDUET) Matlab Lecture 2 2 / 12


Better representation of array along with the appropriate
object

Dr. Muhammad Yousuf Tufail (NEDUET) Matlab Lecture 2 3 / 12


Better representation of array along with the appropriate
object

In the above array, we do not know what height belongs to which person.

Dr. Muhammad Yousuf Tufail (NEDUET) Matlab Lecture 2 3 / 12


Better representation of array along with the appropriate
object

In the above array, we do not know what height belongs to which person.
If height 1,height 2,height 3 and height 4 are the heights of Ali, Jimmy,
Lisa and John respectively.

Dr. Muhammad Yousuf Tufail (NEDUET) Matlab Lecture 2 3 / 12


Better representation of array along with the appropriate
object

In the above array, we do not know what height belongs to which person.
If height 1,height 2,height 3 and height 4 are the heights of Ali, Jimmy,
Lisa and John respectively.
Now, If we want to store this information in the form of array then
following command can be used.

Dr. Muhammad Yousuf Tufail (NEDUET) Matlab Lecture 2 3 / 12


Better representation of array along with the appropriate
object

In the above array, we do not know what height belongs to which person.
If height 1,height 2,height 3 and height 4 are the heights of Ali, Jimmy,
Lisa and John respectively.
Now, If we want to store this information in the form of array then
following command can be used.
fam heights new

Dr. Muhammad Yousuf Tufail (NEDUET) Matlab Lecture 2 3 / 12


Better representation of array along with the appropriate
object

In the above array, we do not know what height belongs to which person.
If height 1,height 2,height 3 and height 4 are the heights of Ali, Jimmy,
Lisa and John respectively.
Now, If we want to store this information in the form of array then
following command can be used.
fam heights new= [[‘Ali ’,num2str(height 1)]

Dr. Muhammad Yousuf Tufail (NEDUET) Matlab Lecture 2 3 / 12


Better representation of array along with the appropriate
object

In the above array, we do not know what height belongs to which person.
If height 1,height 2,height 3 and height 4 are the heights of Ali, Jimmy,
Lisa and John respectively.
Now, If we want to store this information in the form of array then
following command can be used.
fam heights new= [[‘Ali ’,num2str(height 1)],[‘ Jimmy
’,num2str(height 2)]
,[‘ Lisa ’,num2str(height 3)],[‘ John ’,num2str(height 4)]];

Dr. Muhammad Yousuf Tufail (NEDUET) Matlab Lecture 2 3 / 12


Better representation of array along with the appropriate
object

In the above array, we do not know what height belongs to which person.
If height 1,height 2,height 3 and height 4 are the heights of Ali, Jimmy,
Lisa and John respectively.
Now, If we want to store this information in the form of array then
following command can be used.
fam heights new= [[‘Ali ’,num2str(height 1)],[‘ Jimmy
’,num2str(height 2)]
,[‘ Lisa ’,num2str(height 3)],[‘ John ’,num2str(height 4)]];
fam heights new

Dr. Muhammad Yousuf Tufail (NEDUET) Matlab Lecture 2 3 / 12


Better representation of array along with the appropriate
object

In the above array, we do not know what height belongs to which person.
If height 1,height 2,height 3 and height 4 are the heights of Ali, Jimmy,
Lisa and John respectively.
Now, If we want to store this information in the form of array then
following command can be used.
fam heights new= [[‘Ali ’,num2str(height 1)],[‘ Jimmy
’,num2str(height 2)]
,[‘ Lisa ’,num2str(height 3)],[‘ John ’,num2str(height 4)]];
fam heights new
‘Ali 1.64 Jimmy 1.42 Lisa 1.56 John 1.80’

Dr. Muhammad Yousuf Tufail (NEDUET) Matlab Lecture 2 3 / 12


Working with the arrays
fam heights new= [[‘Ali ’,num2str(height 1)],[‘ Jimmy
’,num2str(height 2)],[‘ Lisa ’,num2str(height 3)],[‘ John ’,
num2str(height 4)]]

Dr. Muhammad Yousuf Tufail (NEDUET) Matlab Lecture 2 4 / 12


Working with the arrays
fam heights new= [[‘Ali ’,num2str(height 1)],[‘ Jimmy
’,num2str(height 2)],[‘ Lisa ’,num2str(height 3)],[‘ John ’,
num2str(height 4)]]

Calling entries from the array

Dr. Muhammad Yousuf Tufail (NEDUET) Matlab Lecture 2 4 / 12


Working with the arrays
fam heights new= [[‘Ali ’,num2str(height 1)],[‘ Jimmy
’,num2str(height 2)],[‘ Lisa ’,num2str(height 3)],[‘ John ’,
num2str(height 4)]]

Calling entries from the array


You can always call anything from the array. Here we have few examples
as below.
Example (1)

Dr. Muhammad Yousuf Tufail (NEDUET) Matlab Lecture 2 4 / 12


Working with the arrays
fam heights new= [[‘Ali ’,num2str(height 1)],[‘ Jimmy
’,num2str(height 2)],[‘ Lisa ’,num2str(height 3)],[‘ John ’,
num2str(height 4)]]

Calling entries from the array


You can always call anything from the array. Here we have few examples
as below.
Example (1)
Your task is to call Ali,1.64 and Jimmy from fam heights new.

Dr. Muhammad Yousuf Tufail (NEDUET) Matlab Lecture 2 4 / 12


Working with the arrays
fam heights new= [[‘Ali ’,num2str(height 1)],[‘ Jimmy
’,num2str(height 2)],[‘ Lisa ’,num2str(height 3)],[‘ John ’,
num2str(height 4)]]

Calling entries from the array


You can always call anything from the array. Here we have few examples
as below.
Example (1)
Your task is to call Ali,1.64 and Jimmy from fam heights new.

Solution

Dr. Muhammad Yousuf Tufail (NEDUET) Matlab Lecture 2 4 / 12


Working with the arrays
fam heights new= [[‘Ali ’,num2str(height 1)],[‘ Jimmy
’,num2str(height 2)],[‘ Lisa ’,num2str(height 3)],[‘ John ’,
num2str(height 4)]]

Calling entries from the array


You can always call anything from the array. Here we have few examples
as below.
Example (1)
Your task is to call Ali,1.64 and Jimmy from fam heights new.

Solution
To call the entries from the array, you have to call the required positions.

Dr. Muhammad Yousuf Tufail (NEDUET) Matlab Lecture 2 4 / 12


Working with the arrays
fam heights new= [[‘Ali ’,num2str(height 1)],[‘ Jimmy
’,num2str(height 2)],[‘ Lisa ’,num2str(height 3)],[‘ John ’,
num2str(height 4)]]

Calling entries from the array


You can always call anything from the array. Here we have few examples
as below.
Example (1)
Your task is to call Ali,1.64 and Jimmy from fam heights new.

Solution
To call the entries from the array, you have to call the required positions.
fam heights new(1:14)

Dr. Muhammad Yousuf Tufail (NEDUET) Matlab Lecture 2 4 / 12


Working with the arrays
fam heights new= [[‘Ali ’,num2str(height 1)],[‘ Jimmy
’,num2str(height 2)],[‘ Lisa ’,num2str(height 3)],[‘ John ’,
num2str(height 4)]]

Calling entries from the array


You can always call anything from the array. Here we have few examples
as below.
Example (1)
Your task is to call Ali,1.64 and Jimmy from fam heights new.

Solution
To call the entries from the array, you have to call the required positions.
fam heights new(1:14)
‘Ali 1.64 Jimmy’
Dr. Muhammad Yousuf Tufail (NEDUET) Matlab Lecture 2 4 / 12
Working with the arrays

Dr. Muhammad Yousuf Tufail (NEDUET) Matlab Lecture 2 5 / 12


Working with the arrays

Example (2)

Dr. Muhammad Yousuf Tufail (NEDUET) Matlab Lecture 2 5 / 12


Working with the arrays

Example (2)
Find the second element in the fam heights new

Dr. Muhammad Yousuf Tufail (NEDUET) Matlab Lecture 2 5 / 12


Working with the arrays

Example (2)
Find the second element in the fam heights new

Solution

Dr. Muhammad Yousuf Tufail (NEDUET) Matlab Lecture 2 5 / 12


Working with the arrays

Example (2)
Find the second element in the fam heights new

Solution
fam heights new(10:19)

Dr. Muhammad Yousuf Tufail (NEDUET) Matlab Lecture 2 5 / 12


Working with the arrays

Example (2)
Find the second element in the fam heights new

Solution
fam heights new(10:19)
‘Jimmy 1.42’

Dr. Muhammad Yousuf Tufail (NEDUET) Matlab Lecture 2 5 / 12


Working with the arrays

Dr. Muhammad Yousuf Tufail (NEDUET) Matlab Lecture 2 6 / 12


Working with the arrays

Example (3)

Dr. Muhammad Yousuf Tufail (NEDUET) Matlab Lecture 2 6 / 12


Working with the arrays

Example (3)
Display all the values from fam heights new except first. i.e., Ali and his
height must not be printed.

Dr. Muhammad Yousuf Tufail (NEDUET) Matlab Lecture 2 6 / 12


Working with the arrays

Example (3)
Display all the values from fam heights new except first. i.e., Ali and his
height must not be printed.

Solution

Dr. Muhammad Yousuf Tufail (NEDUET) Matlab Lecture 2 6 / 12


Working with the arrays

Example (3)
Display all the values from fam heights new except first. i.e., Ali and his
height must not be printed.

Solution
There are two ways for this questions that are as below:

Dr. Muhammad Yousuf Tufail (NEDUET) Matlab Lecture 2 6 / 12


Working with the arrays

Example (3)
Display all the values from fam heights new except first. i.e., Ali and his
height must not be printed.

Solution
There are two ways for this questions that are as below:
Method 1:

Dr. Muhammad Yousuf Tufail (NEDUET) Matlab Lecture 2 6 / 12


Working with the arrays

Example (3)
Display all the values from fam heights new except first. i.e., Ali and his
height must not be printed.

Solution
There are two ways for this questions that are as below:
Method 1:
fam heights new(10:38)
Method 2:

Dr. Muhammad Yousuf Tufail (NEDUET) Matlab Lecture 2 6 / 12


Working with the arrays

Example (3)
Display all the values from fam heights new except first. i.e., Ali and his
height must not be printed.

Solution
There are two ways for this questions that are as below:
Method 1:
fam heights new(10:38)
Method 2:
fam heights new(10:end)

Dr. Muhammad Yousuf Tufail (NEDUET) Matlab Lecture 2 6 / 12


Working with the arrays

Example (3)
Display all the values from fam heights new except first. i.e., Ali and his
height must not be printed.

Solution
There are two ways for this questions that are as below:
Method 1:
fam heights new(10:38)
Method 2:
fam heights new(10:end)

This second method is more appropriate than first.

Dr. Muhammad Yousuf Tufail (NEDUET) Matlab Lecture 2 6 / 12


Working with the arrays

Example (3)
Display all the values from fam heights new except first. i.e., Ali and his
height must not be printed.

Solution
There are two ways for this questions that are as below:
Method 1:
fam heights new(10:38)
Method 2:
fam heights new(10:end)

This second method is more appropriate than first.


WHY?

Dr. Muhammad Yousuf Tufail (NEDUET) Matlab Lecture 2 6 / 12


Working with the arrays

Example (3)
Display all the values from fam heights new except first. i.e., Ali and his
height must not be printed.

Solution
There are two ways for this questions that are as below:
Method 1:
fam heights new(10:38)
Method 2:
fam heights new(10:end)

This second method is more appropriate than first.


WHY?
Ans. Because (in first method) you need to know how many entries in the
array. However there is no such requirement in the second method.
Dr. Muhammad Yousuf Tufail (NEDUET) Matlab Lecture 2 6 / 12
Some built-in Matlab functions

Dr. Muhammad Yousuf Tufail (NEDUET) Matlab Lecture 2 7 / 12


Some built-in Matlab functions

Maximum function:

Dr. Muhammad Yousuf Tufail (NEDUET) Matlab Lecture 2 7 / 12


Some built-in Matlab functions

Maximum function:This function is used to calculate the maximum value


in the array.

Dr. Muhammad Yousuf Tufail (NEDUET) Matlab Lecture 2 7 / 12


Some built-in Matlab functions

Maximum function:This function is used to calculate the maximum value


in the array.
For example:

Dr. Muhammad Yousuf Tufail (NEDUET) Matlab Lecture 2 7 / 12


Some built-in Matlab functions

Maximum function:This function is used to calculate the maximum value


in the array.
For example:
A=[20,2,30,1.2]

Dr. Muhammad Yousuf Tufail (NEDUET) Matlab Lecture 2 7 / 12


Some built-in Matlab functions

Maximum function:This function is used to calculate the maximum value


in the array.
For example:
A=[20,2,30,1.2]
max(A)

Dr. Muhammad Yousuf Tufail (NEDUET) Matlab Lecture 2 7 / 12


Some built-in Matlab functions

Maximum function:This function is used to calculate the maximum value


in the array.
For example:
A=[20,2,30,1.2]
max(A)
30

Dr. Muhammad Yousuf Tufail (NEDUET) Matlab Lecture 2 7 / 12


Some built-in Matlab functions

Maximum function:This function is used to calculate the maximum value


in the array.
For example:
A=[20,2,30,1.2]
max(A)
30
Minimum function:

Dr. Muhammad Yousuf Tufail (NEDUET) Matlab Lecture 2 7 / 12


Some built-in Matlab functions

Maximum function:This function is used to calculate the maximum value


in the array.
For example:
A=[20,2,30,1.2]
max(A)
30
Minimum function:This function is used to calculate the minimum value
in the array.

Dr. Muhammad Yousuf Tufail (NEDUET) Matlab Lecture 2 7 / 12


Some built-in Matlab functions

Maximum function:This function is used to calculate the maximum value


in the array.
For example:
A=[20,2,30,1.2]
max(A)
30
Minimum function:This function is used to calculate the minimum value
in the array.
min(A)

Dr. Muhammad Yousuf Tufail (NEDUET) Matlab Lecture 2 7 / 12


Some built-in Matlab functions

Maximum function:This function is used to calculate the maximum value


in the array.
For example:
A=[20,2,30,1.2]
max(A)
30
Minimum function:This function is used to calculate the minimum value
in the array.
min(A)
1.2

Dr. Muhammad Yousuf Tufail (NEDUET) Matlab Lecture 2 7 / 12


Some built-in Matlab functions

Maximum function:This function is used to calculate the maximum value


in the array.
For example:
A=[20,2,30,1.2]
max(A)
30
Minimum function:This function is used to calculate the minimum value
in the array.
min(A)
1.2
Round function:

Dr. Muhammad Yousuf Tufail (NEDUET) Matlab Lecture 2 7 / 12


Some built-in Matlab functions

Maximum function:This function is used to calculate the maximum value


in the array.
For example:
A=[20,2,30,1.2]
max(A)
30
Minimum function:This function is used to calculate the minimum value
in the array.
min(A)
1.2
Round function:This function is used to round of the fraction.

Dr. Muhammad Yousuf Tufail (NEDUET) Matlab Lecture 2 7 / 12


Some built-in Matlab functions

Maximum function:This function is used to calculate the maximum value


in the array.
For example:
A=[20,2,30,1.2]
max(A)
30
Minimum function:This function is used to calculate the minimum value
in the array.
min(A)
1.2
Round function:This function is used to round of the fraction.For
example a=5.462

Dr. Muhammad Yousuf Tufail (NEDUET) Matlab Lecture 2 7 / 12


Some built-in Matlab functions

Maximum function:This function is used to calculate the maximum value


in the array.
For example:
A=[20,2,30,1.2]
max(A)
30
Minimum function:This function is used to calculate the minimum value
in the array.
min(A)
1.2
Round function:This function is used to round of the fraction.For
example a=5.462
round(a)

Dr. Muhammad Yousuf Tufail (NEDUET) Matlab Lecture 2 7 / 12


Some built-in Matlab functions

Maximum function:This function is used to calculate the maximum value


in the array.
For example:
A=[20,2,30,1.2]
max(A)
30
Minimum function:This function is used to calculate the minimum value
in the array.
min(A)
1.2
Round function:This function is used to round of the fraction.For
example a=5.462
round(a)
5

Dr. Muhammad Yousuf Tufail (NEDUET) Matlab Lecture 2 7 / 12


Some built-in Matlab functions

Maximum function:This function is used to calculate the maximum value


in the array.
For example:
A=[20,2,30,1.2]
max(A)
30
Minimum function:This function is used to calculate the minimum value
in the array.
min(A)
1.2
Round function:This function is used to round of the fraction.For
example a=5.462
round(a)
5

Dr. Muhammad Yousuf Tufail (NEDUET) Matlab Lecture 2 7 / 12


Some built-in Matlab function

Dr. Muhammad Yousuf Tufail (NEDUET) Matlab Lecture 2 8 / 12


Some built-in Matlab function

You can also round off the fraction upto any decimal place of your choice.

Dr. Muhammad Yousuf Tufail (NEDUET) Matlab Lecture 2 8 / 12


Some built-in Matlab function

You can also round off the fraction upto any decimal place of your choice.
For example,

Dr. Muhammad Yousuf Tufail (NEDUET) Matlab Lecture 2 8 / 12


Some built-in Matlab function

You can also round off the fraction upto any decimal place of your choice.
For example, ‘a’ can be round off upto 1 decimal place as below:

Dr. Muhammad Yousuf Tufail (NEDUET) Matlab Lecture 2 8 / 12


Some built-in Matlab function

You can also round off the fraction upto any decimal place of your choice.
For example, ‘a’ can be round off upto 1 decimal place as below:
round(a,1)

Dr. Muhammad Yousuf Tufail (NEDUET) Matlab Lecture 2 8 / 12


Some built-in Matlab function

You can also round off the fraction upto any decimal place of your choice.
For example, ‘a’ can be round off upto 1 decimal place as below:
round(a,1)
5.5

Dr. Muhammad Yousuf Tufail (NEDUET) Matlab Lecture 2 8 / 12


Matrix/Array representation in more
than one dimension

Dr. Muhammad Yousuf Tufail (NEDUET) Matlab Lecture 2 9 / 12


Matrix/Array representation in more
than one dimension
Matlab provides you an option to create a matrix of any order.

Dr. Muhammad Yousuf Tufail (NEDUET) Matlab Lecture 2 9 / 12


Matrix/Array representation in more
than one dimension
Matlab provides you an option to create a matrix of any order.
Following example will illustrate this fact.

Dr. Muhammad Yousuf Tufail (NEDUET) Matlab Lecture 2 9 / 12


Matrix/Array representation in more
than one dimension
Matlab provides you an option to create a matrix of any order.
Following example will illustrate this fact.
Example (4)

Dr. Muhammad Yousuf Tufail (NEDUET) Matlab Lecture 2 9 / 12


Matrix/Array representation in more
than one dimension
Matlab provides you an option to create a matrix of any order.
Following example will illustrate this fact.
Example (4)
Construct a following matrix in your script.
 
1 4 5
2 9 8
A= 2

9 9
−1 −4 −5

Dr. Muhammad Yousuf Tufail (NEDUET) Matlab Lecture 2 9 / 12


Matrix/Array representation in more
than one dimension
Matlab provides you an option to create a matrix of any order.
Following example will illustrate this fact.
Example (4)
Construct a following matrix in your script.
 
1 4 5
2 9 8
A= 2

9 9
−1 −4 −5

Solution

Dr. Muhammad Yousuf Tufail (NEDUET) Matlab Lecture 2 9 / 12


Matrix/Array representation in more
than one dimension
Matlab provides you an option to create a matrix of any order.
Following example will illustrate this fact.
Example (4)
Construct a following matrix in your script.
 
1 4 5
2 9 8
A= 2

9 9
−1 −4 −5

Solution
A=[1 4 5;2 9 8;2 9 9;-1 -4 -5];

Dr. Muhammad Yousuf Tufail (NEDUET) Matlab Lecture 2 9 / 12


Matrix/Array representation in more
than one dimension
Matlab provides you an option to create a matrix of any order.
Following example will illustrate this fact.
Example (4)
Construct a following matrix in your script.
 
1 4 5
2 9 8
A= 2

9 9
−1 −4 −5

Solution
A=[1 4 5;2 9 8;2 9 9;-1 -4 -5];
A
You will get the desired matrix
Dr. Muhammad Yousuf Tufail (NEDUET) Matlab Lecture 2 9 / 12
Calling the elements from the array

Dr. Muhammad Yousuf Tufail (NEDUET) Matlab Lecture 2 10 / 12


Calling the elements from the array
Example (5)
Display 8 from the matrix.

Dr. Muhammad Yousuf Tufail (NEDUET) Matlab Lecture 2 10 / 12


Calling the elements from the array
Example (5)
Display 8 from the matrix.

Solution

Dr. Muhammad Yousuf Tufail (NEDUET) Matlab Lecture 2 10 / 12


Calling the elements from the array
Example (5)
Display 8 from the matrix.

Solution
As 8 is given in the second row and third column.

Dr. Muhammad Yousuf Tufail (NEDUET) Matlab Lecture 2 10 / 12


Calling the elements from the array
Example (5)
Display 8 from the matrix.

Solution
As 8 is given in the second row and third column. Therefore desired result
will be:

Dr. Muhammad Yousuf Tufail (NEDUET) Matlab Lecture 2 10 / 12


Calling the elements from the array
Example (5)
Display 8 from the matrix.

Solution
As 8 is given in the second row and third column. Therefore desired result
will be:
A(2,3)

Dr. Muhammad Yousuf Tufail (NEDUET) Matlab Lecture 2 10 / 12


Calling the elements from the array
Example (5)
Display 8 from the matrix.

Solution
As 8 is given in the second row and third column. Therefore desired result
will be:
A(2,3)
8

Dr. Muhammad Yousuf Tufail (NEDUET) Matlab Lecture 2 10 / 12


Calling the elements from the array
Example (5)
Display 8 from the matrix.

Solution
As 8 is given in the second row and third column. Therefore desired result
will be:
A(2,3)
8

Example (6)
Display -4 from the matrix

Dr. Muhammad Yousuf Tufail (NEDUET) Matlab Lecture 2 10 / 12


Calling the elements from the array
Example (5)
Display 8 from the matrix.

Solution
As 8 is given in the second row and third column. Therefore desired result
will be:
A(2,3)
8

Example (6)
Display -4 from the matrix

Solution
A(4,2)
-4
Dr. Muhammad Yousuf Tufail (NEDUET) Matlab Lecture 2 10 / 12
Calling the elements from the array

Dr. Muhammad Yousuf Tufail (NEDUET) Matlab Lecture 2 11 / 12


Calling the elements from the array
Example (7)

Dr. Muhammad Yousuf Tufail (NEDUET) Matlab Lecture 2 11 / 12


Calling the elements from the array
Example (7)
Call whole second row.

Dr. Muhammad Yousuf Tufail (NEDUET) Matlab Lecture 2 11 / 12


Calling the elements from the array
Example (7)
Call whole second row.

Solution

Dr. Muhammad Yousuf Tufail (NEDUET) Matlab Lecture 2 11 / 12


Calling the elements from the array
Example (7)
Call whole second row.

Solution
A(2,:)

Dr. Muhammad Yousuf Tufail (NEDUET) Matlab Lecture 2 11 / 12


Calling the elements from the array
Example (7)
Call whole second row.

Solution
A(2,:)
2 9 8

Dr. Muhammad Yousuf Tufail (NEDUET) Matlab Lecture 2 11 / 12


Calling the elements from the array
Example (7)
Call whole second row.

Solution
A(2,:)
2 9 8

Example (8)
Display third column from the matrix

Dr. Muhammad Yousuf Tufail (NEDUET) Matlab Lecture 2 11 / 12


Calling the elements from the array
Example (7)
Call whole second row.

Solution
A(2,:)
2 9 8

Example (8)
Display third column from the matrix

Solution
A(:,3)

Dr. Muhammad Yousuf Tufail (NEDUET) Matlab Lecture 2 11 / 12


Calling the elements from the array
Example (7)
Call whole second row.

Solution
A(2,:)
2 9 8

Example (8)
Display third column from the matrix

Solution
A(:,3)
5
8
9
-5
Dr. Muhammad Yousuf Tufail (NEDUET) Matlab Lecture 2 11 / 12
Practice questions

Dr. Muhammad Yousuf Tufail (NEDUET) Matlab Lecture 2 12 / 12


Practice questions

Example (practice)
Call third row
Call fourth column
Call -5 from the matrix
Call first three values of the third row
Call last three values in the fourth column

Dr. Muhammad Yousuf Tufail (NEDUET) Matlab Lecture 2 12 / 12

You might also like