SlideShare ist ein Scribd-Unternehmen logo
1 von 14
Downloaden Sie, um offline zu lesen
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

Weitere ähnliche Inhalte

Was ist angesagt?

Dave Kovalcik Grade Report from RMU
Dave Kovalcik Grade Report from RMUDave Kovalcik Grade Report from RMU
Dave Kovalcik Grade Report from RMU
Dave Kovalcik
 

Was ist angesagt? (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...
 

Andere mochten auch

Andere mochten auch (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
 

Ähnlich wie matab no3

Sql based paperless examination system
Sql based paperless examination systemSql based paperless examination system
Sql based paperless examination system
Alexander Decker
 

Ähnlich wie 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
 

Kürzlich hochgeladen

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
QucHHunhnh
 

Kürzlich hochgeladen (20)

Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
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
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 

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