SlideShare una empresa de Scribd logo
1 de 14
Descargar para leer sin conexión
Lab No.03

Structures, logical and
relational operators

Designed by : Dawar Awan
dawar@cecos.edu.pk
CECOS College of Engineering and IT

March – July 2012
Relational and logical operators
Relational operators

<
>
<=
>=
==
~=

less than
greater than
less than or equal
greater than or equal
equal
not equal

 Their result will be either true (1) or false (0)
 >> 3 < 5

returns 1

 >> a = 3 == 5

returns 0

 These operators when applied on matrices, returns 1s and 0s in
a resultant matrix
CECOS College of Engineering and IT

March – July 2012
Relational and logical operators
>> A = [ 1 2; 3 4 ];
>> B = [ 6 7; 8 9 ];
>> A == B
ans =
0 0

0 0
>> A < B
ans =
1 1
1 1
CECOS College of Engineering and IT

March – July 2012
Relational and logical operators
Logical operators

&

and

|

or

~

not

 Try the following commands
>> ~A

>> A | ~A

>> A&B

>> A | B

>> A & ~B
CECOS College of Engineering and IT

March – July 2012
Control structures
 Structures are used to control the sequence of
execution of code. This is achieved by using
 Conditional control structures

 Looping control structures
 Logical and relational operators are used in
implementing the control structures.
CECOS College of Engineering and IT

March – July 2012
Conditional Control structures
 Following are some conditional/branching control
structures
i- if-end Construct : The most basic construct is
if <condition>
<program>

end

Example
a = 1;

b = 2;
if a < b
c = 3;
end;
CECOS College of Engineering and IT

March – July 2012
Conditional Control structures
ii- If-else-end Construct
if <condition1>
<program1>
else

Example
a = 1;
b = 2;

<program2>

if a < b

end

c = 3;
else
c=10;
end;

CECOS College of Engineering and IT

March – July 2012
Conditional Control structures
iii- If-elseif-end Construct: Another variation is
if <condition1>
<program1>
elseif <condition2>
<program2>
end

CECOS College of Engineering and IT

Example
a = 1;
b = 2;
if a < b
disp(‘b is greater’);
elseif (a>b)
disp(‘a is greater’);
else
disp(‘ a and b are equal’);
end;
March – July 2012
Looping Control structures
i- For loop
for i= 1 : n
<program>
end
For each value of ‘i’ , “program” executes one time i.e the program
will execute n-times
>> for i = 1 : 5

c = 2*i
end
c=2

c = 4 ….

CECOS College of Engineering and IT

… ….

…. c = 10
March – July 2012
Looping Control structures
Any vector can be used for the value of i

for i = [2,4,5,6,10]
<program>
end

CECOS College of Engineering and IT

March – July 2012
Looping Control structures
ii- Nested For loop

for i=1:3
for j=1:3
A(i,j) = i+j;

Program

end
end
For each value of ‘i’ , “program” executes three times i.e the
program will execute 9 times.

A(1,1)=1+1

A(1,2)=1+2

A(1,3)=1+3

A(2,1)=2+1

……

….

…

A(3,3)=3+3

…..

CECOS College of Engineering and IT

…..

….

March – July 2012
While loops
while <condition>
<program>
end
 The ‘program’ executes till the ‘condition’ remains true,
and stops when the ‘condition’ becomes false

CECOS College of Engineering and IT

March – July 2012
Task
1. Create an m-file that finds the factorial of a number using for
loop (don’t use the built in function ‘factorial’).
2. Repeat the above question for while loop.
3. Generate square of first ten integers using for loop.
4. Add the following two matrices using nested for loop.

CECOS College of Engineering and IT

March – July 2012
Task
5. Using MATLAB, Find the value of C, where C is defined as
follows:

C= 10a

,

0<a<=5

C= 20a

,

5<a<=10

The value of ‘a’ shall be provided by the user.

6. Using MATLAB, List all the values of y, for 0 ≤ n ≤ 10. if

y=n2

,

n is even

y=n

,

n is odd

CECOS College of Engineering and IT

March – July 2012

Más contenido relacionado

La actualidad más candente

Modeling Style and Delay Model of VHDL By Ap
Modeling Style and Delay Model of VHDL By ApModeling Style and Delay Model of VHDL By Ap
Modeling Style and Delay Model of VHDL By ApEr. Ashish Pandey
 
Concatenate function in Excel - How to combine cell in Excel
Concatenate function in Excel - How to combine cell in ExcelConcatenate function in Excel - How to combine cell in Excel
Concatenate function in Excel - How to combine cell in ExcelSamantha Simon
 
Inconsistencies in Models of Adaptive Service Robots
Inconsistencies in Models of Adaptive Service RobotsInconsistencies in Models of Adaptive Service Robots
Inconsistencies in Models of Adaptive Service RobotsIvan Ruchkin
 
Saie saraf resume_2
Saie saraf resume_2Saie saraf resume_2
Saie saraf resume_2Saie Saraf
 
Application's of Numerical Math in CSE
Application's of Numerical Math in CSEApplication's of Numerical Math in CSE
Application's of Numerical Math in CSEsanjana mun
 
Tte 451 operations research fall 2021 part 1
Tte 451  operations research   fall 2021   part 1Tte 451  operations research   fall 2021   part 1
Tte 451 operations research fall 2021 part 1Wael ElDessouki
 
My presentation
My presentationMy presentation
My presentationSaifur13
 
Yasar University Linear Algebra Calculator
Yasar University Linear Algebra CalculatorYasar University Linear Algebra Calculator
Yasar University Linear Algebra CalculatorYusuf Yasin Kumbul
 
Dave Kovalcik Grade Report from RMU
Dave Kovalcik Grade Report from RMUDave Kovalcik Grade Report from RMU
Dave Kovalcik Grade Report from RMUDave Kovalcik
 
Selenium Test Automation series - Data Driven Testing-From Excel Sheet
Selenium Test Automation series - Data Driven Testing-From Excel SheetSelenium Test Automation series - Data Driven Testing-From Excel Sheet
Selenium Test Automation series - Data Driven Testing-From Excel SheetTurgay Koklu
 
Refutations on "Debunking the Myths of Influence Maximization: An In-Depth Be...
Refutations on "Debunking the Myths of Influence Maximization: An In-Depth Be...Refutations on "Debunking the Myths of Influence Maximization: An In-Depth Be...
Refutations on "Debunking the Myths of Influence Maximization: An In-Depth Be...Wei Lu
 

La actualidad más candente (20)

Function point analysis
Function point analysisFunction point analysis
Function point analysis
 
Xi practical file
Xi practical fileXi practical file
Xi practical file
 
LAB 1 Report.docx
LAB 1 Report.docxLAB 1 Report.docx
LAB 1 Report.docx
 
LAB 2 Report.docx
LAB 2 Report.docxLAB 2 Report.docx
LAB 2 Report.docx
 
Modeling Style and Delay Model of VHDL By Ap
Modeling Style and Delay Model of VHDL By ApModeling Style and Delay Model of VHDL By Ap
Modeling Style and Delay Model of VHDL By Ap
 
Concatenate function in Excel - How to combine cell in Excel
Concatenate function in Excel - How to combine cell in ExcelConcatenate function in Excel - How to combine cell in Excel
Concatenate function in Excel - How to combine cell in Excel
 
Linear regression model
Linear regression modelLinear regression model
Linear regression model
 
Weekly report 8
Weekly report 8Weekly report 8
Weekly report 8
 
Inconsistencies in Models of Adaptive Service Robots
Inconsistencies in Models of Adaptive Service RobotsInconsistencies in Models of Adaptive Service Robots
Inconsistencies in Models of Adaptive Service Robots
 
Saie saraf resume_2
Saie saraf resume_2Saie saraf resume_2
Saie saraf resume_2
 
res1
res1res1
res1
 
Application's of Numerical Math in CSE
Application's of Numerical Math in CSEApplication's of Numerical Math in CSE
Application's of Numerical Math in CSE
 
PAL
PALPAL
PAL
 
Tte 451 operations research fall 2021 part 1
Tte 451  operations research   fall 2021   part 1Tte 451  operations research   fall 2021   part 1
Tte 451 operations research fall 2021 part 1
 
My presentation
My presentationMy presentation
My presentation
 
Applications of numerical methods
Applications of numerical methodsApplications of numerical methods
Applications of numerical methods
 
Yasar University Linear Algebra Calculator
Yasar University Linear Algebra CalculatorYasar University Linear Algebra Calculator
Yasar University Linear Algebra Calculator
 
Dave Kovalcik Grade Report from RMU
Dave Kovalcik Grade Report from RMUDave Kovalcik Grade Report from RMU
Dave Kovalcik Grade Report from RMU
 
Selenium Test Automation series - Data Driven Testing-From Excel Sheet
Selenium Test Automation series - Data Driven Testing-From Excel SheetSelenium Test Automation series - Data Driven Testing-From Excel Sheet
Selenium Test Automation series - Data Driven Testing-From Excel Sheet
 
Refutations on "Debunking the Myths of Influence Maximization: An In-Depth Be...
Refutations on "Debunking the Myths of Influence Maximization: An In-Depth Be...Refutations on "Debunking the Myths of Influence Maximization: An In-Depth Be...
Refutations on "Debunking the Myths of Influence Maximization: An In-Depth Be...
 

Destacado (9)

matab no9
matab no9matab no9
matab no9
 
Lab no.08
Lab no.08Lab no.08
Lab no.08
 
communication system Chapter 5
communication system Chapter 5communication system Chapter 5
communication system Chapter 5
 
communication system Chapter 6
communication system Chapter 6communication system Chapter 6
communication system Chapter 6
 
communication system ch1
communication system ch1communication system ch1
communication system ch1
 
Lab no.07
Lab no.07Lab no.07
Lab no.07
 
communication system Chapter 3
communication system Chapter 3communication system Chapter 3
communication system Chapter 3
 
communication system Chapter 4
communication system Chapter 4communication system Chapter 4
communication system Chapter 4
 
communication system Chapter 2
communication system Chapter 2communication system Chapter 2
communication system Chapter 2
 

Similar a matab no3

Software Engineering Fundamentals in Computer Science
Software Engineering Fundamentals in Computer ScienceSoftware Engineering Fundamentals in Computer Science
Software Engineering Fundamentals in Computer ScienceArti Parab Academics
 
Eclipse Code Monitoring Utility
Eclipse Code Monitoring UtilityEclipse Code Monitoring Utility
Eclipse Code Monitoring UtilityIRJET Journal
 
Se 381 - lec 25 - 32 - 12 may29 - program size and cost estimation models
Se 381 - lec 25 - 32 - 12 may29 - program size and cost estimation modelsSe 381 - lec 25 - 32 - 12 may29 - program size and cost estimation models
Se 381 - lec 25 - 32 - 12 may29 - program size and cost estimation modelsbabak danyal
 
Exp 02-COCOMO (1).pptx
Exp 02-COCOMO (1).pptxExp 02-COCOMO (1).pptx
Exp 02-COCOMO (1).pptxYagnaGummadi
 
ЄРМЕК КАДИРБАЄВ & АЛЕКС РИБКІН «How we train QAEs to join automation» Online ...
ЄРМЕК КАДИРБАЄВ & АЛЕКС РИБКІН «How we train QAEs to join automation» Online ...ЄРМЕК КАДИРБАЄВ & АЛЕКС РИБКІН «How we train QAEs to join automation» Online ...
ЄРМЕК КАДИРБАЄВ & АЛЕКС РИБКІН «How we train QAEs to join automation» Online ...GoQA
 
Problem Solving Techniques and Introduction to C
Problem Solving Techniques and Introduction to CProblem Solving Techniques and Introduction to C
Problem Solving Techniques and Introduction to CPrabu U
 
COCOMO Model For Effort Estimation
COCOMO Model For Effort EstimationCOCOMO Model For Effort Estimation
COCOMO Model For Effort Estimationgrandhiprasuna
 
Software Project Planning 1
Software Project Planning 1Software Project Planning 1
Software Project Planning 1Gagan Deep
 
Sql based paperless examination system
Sql based paperless examination systemSql based paperless examination system
Sql based paperless examination systemAlexander Decker
 
MANOJ H internship ppt.pptx
MANOJ H internship ppt.pptxMANOJ H internship ppt.pptx
MANOJ H internship ppt.pptxManishShah785043
 
IRJET- Value Management
IRJET- Value ManagementIRJET- Value Management
IRJET- Value ManagementIRJET Journal
 
COCOMO methods for software size estimation
COCOMO methods for software size estimationCOCOMO methods for software size estimation
COCOMO methods for software size estimationPramod Parajuli
 
Chapter 6 software metrics
Chapter 6 software metricsChapter 6 software metrics
Chapter 6 software metricsdespicable me
 
Synopsis on Online examination system using php
Synopsis on Online examination system using phpSynopsis on Online examination system using php
Synopsis on Online examination system using phpArchana Jha
 

Similar a matab no3 (20)

Software Engineering Fundamentals in Computer Science
Software Engineering Fundamentals in Computer ScienceSoftware Engineering Fundamentals in Computer Science
Software Engineering Fundamentals in Computer Science
 
Eclipse Code Monitoring Utility
Eclipse Code Monitoring UtilityEclipse Code Monitoring Utility
Eclipse Code Monitoring Utility
 
Se 381 - lec 25 - 32 - 12 may29 - program size and cost estimation models
Se 381 - lec 25 - 32 - 12 may29 - program size and cost estimation modelsSe 381 - lec 25 - 32 - 12 may29 - program size and cost estimation models
Se 381 - lec 25 - 32 - 12 may29 - program size and cost estimation models
 
Exp 02-COCOMO (1).pptx
Exp 02-COCOMO (1).pptxExp 02-COCOMO (1).pptx
Exp 02-COCOMO (1).pptx
 
ЄРМЕК КАДИРБАЄВ & АЛЕКС РИБКІН «How we train QAEs to join automation» Online ...
ЄРМЕК КАДИРБАЄВ & АЛЕКС РИБКІН «How we train QAEs to join automation» Online ...ЄРМЕК КАДИРБАЄВ & АЛЕКС РИБКІН «How we train QAEs to join automation» Online ...
ЄРМЕК КАДИРБАЄВ & АЛЕКС РИБКІН «How we train QAEs to join automation» Online ...
 
Cocomomodel
CocomomodelCocomomodel
Cocomomodel
 
COCOMO Model
COCOMO ModelCOCOMO Model
COCOMO Model
 
Cocomo model
Cocomo modelCocomo model
Cocomo model
 
Problem Solving Techniques and Introduction to C
Problem Solving Techniques and Introduction to CProblem Solving Techniques and Introduction to C
Problem Solving Techniques and Introduction to C
 
COCOMO Model For Effort Estimation
COCOMO Model For Effort EstimationCOCOMO Model For Effort Estimation
COCOMO Model For Effort Estimation
 
Software Project Planning 1
Software Project Planning 1Software Project Planning 1
Software Project Planning 1
 
Ch15-22-23 (1).ppt
Ch15-22-23 (1).pptCh15-22-23 (1).ppt
Ch15-22-23 (1).ppt
 
Resume BI and DataAnalyst
Resume BI and DataAnalystResume BI and DataAnalyst
Resume BI and DataAnalyst
 
Sql based paperless examination system
Sql based paperless examination systemSql based paperless examination system
Sql based paperless examination system
 
MANOJ H internship ppt.pptx
MANOJ H internship ppt.pptxMANOJ H internship ppt.pptx
MANOJ H internship ppt.pptx
 
IRJET- Value Management
IRJET- Value ManagementIRJET- Value Management
IRJET- Value Management
 
ISS_1
ISS_1ISS_1
ISS_1
 
COCOMO methods for software size estimation
COCOMO methods for software size estimationCOCOMO methods for software size estimation
COCOMO methods for software size estimation
 
Chapter 6 software metrics
Chapter 6 software metricsChapter 6 software metrics
Chapter 6 software metrics
 
Synopsis on Online examination system using php
Synopsis on Online examination system using phpSynopsis on Online examination system using php
Synopsis on Online examination system using php
 

Último

How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17Celine George
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the ClassroomPooky Knightsmith
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxDr. Sarita Anand
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024Elizabeth Walsh
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...ZurliaSoop
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxcallscotland1987
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibitjbellavia9
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseAnaAcapella
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxAmanpreet Kaur
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.MaryamAhmad92
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdfssuserdda66b
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 

Último (20)

How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptx
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 

matab no3

  • 1. Lab No.03 Structures, logical and relational operators Designed by : Dawar Awan dawar@cecos.edu.pk CECOS College of Engineering and IT March – July 2012
  • 2. Relational and logical operators Relational operators < > <= >= == ~= less than greater than less than or equal greater than or equal equal not equal  Their result will be either true (1) or false (0)  >> 3 < 5 returns 1  >> a = 3 == 5 returns 0  These operators when applied on matrices, returns 1s and 0s in a resultant matrix CECOS College of Engineering and IT March – July 2012
  • 3. Relational and logical operators >> A = [ 1 2; 3 4 ]; >> B = [ 6 7; 8 9 ]; >> A == B ans = 0 0 0 0 >> A < B ans = 1 1 1 1 CECOS College of Engineering and IT March – July 2012
  • 4. Relational and logical operators Logical operators & and | or ~ not  Try the following commands >> ~A >> A | ~A >> A&B >> A | B >> A & ~B CECOS College of Engineering and IT March – July 2012
  • 5. Control structures  Structures are used to control the sequence of execution of code. This is achieved by using  Conditional control structures  Looping control structures  Logical and relational operators are used in implementing the control structures. CECOS College of Engineering and IT March – July 2012
  • 6. Conditional Control structures  Following are some conditional/branching control structures i- if-end Construct : The most basic construct is if <condition> <program> end Example a = 1; b = 2; if a < b c = 3; end; CECOS College of Engineering and IT March – July 2012
  • 7. Conditional Control structures ii- If-else-end Construct if <condition1> <program1> else Example a = 1; b = 2; <program2> if a < b end c = 3; else c=10; end; CECOS College of Engineering and IT March – July 2012
  • 8. Conditional Control structures iii- If-elseif-end Construct: Another variation is if <condition1> <program1> elseif <condition2> <program2> end CECOS College of Engineering and IT Example a = 1; b = 2; if a < b disp(‘b is greater’); elseif (a>b) disp(‘a is greater’); else disp(‘ a and b are equal’); end; March – July 2012
  • 9. Looping Control structures i- For loop for i= 1 : n <program> end For each value of ‘i’ , “program” executes one time i.e the program will execute n-times >> for i = 1 : 5 c = 2*i end c=2 c = 4 …. CECOS College of Engineering and IT … …. …. c = 10 March – July 2012
  • 10. Looping Control structures Any vector can be used for the value of i for i = [2,4,5,6,10] <program> end CECOS College of Engineering and IT March – July 2012
  • 11. Looping Control structures ii- Nested For loop for i=1:3 for j=1:3 A(i,j) = i+j; Program end end For each value of ‘i’ , “program” executes three times i.e the program will execute 9 times. A(1,1)=1+1 A(1,2)=1+2 A(1,3)=1+3 A(2,1)=2+1 …… …. … A(3,3)=3+3 ….. CECOS College of Engineering and IT ….. …. March – July 2012
  • 12. While loops while <condition> <program> end  The ‘program’ executes till the ‘condition’ remains true, and stops when the ‘condition’ becomes false CECOS College of Engineering and IT March – July 2012
  • 13. Task 1. Create an m-file that finds the factorial of a number using for loop (don’t use the built in function ‘factorial’). 2. Repeat the above question for while loop. 3. Generate square of first ten integers using for loop. 4. Add the following two matrices using nested for loop. CECOS College of Engineering and IT March – July 2012
  • 14. Task 5. Using MATLAB, Find the value of C, where C is defined as follows: C= 10a , 0<a<=5 C= 20a , 5<a<=10 The value of ‘a’ shall be provided by the user. 6. Using MATLAB, List all the values of y, for 0 ≤ n ≤ 10. if y=n2 , n is even y=n , n is odd CECOS College of Engineering and IT March – July 2012