Upcoming SlideShare
×

# matab no3

210 views
122 views

Published on

Published in: Education
1 Like
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

Views
Total views
210
On SlideShare
0
From Embeds
0
Number of Embeds
9
Actions
Shares
0
4
0
Likes
1
Embeds 0
No embeds

No notes for slide

### matab no3

1. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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