Download presentation
Presentation is loading. Please wait.
1
Lecture 3 Sept 3 Complete Chapter 3 Exercises quiz
2
String operations >> length(greeting) ans = 5 >> strcmp(greeting, ‘hello!’) Ans = 0 ans = 1 if the Boolean expression is true, 0 else. strcmp compares two strings. Strings are case-sensitive. strcat concatenates (glues) strings
3
String operations strcmp(w1, w2) returns 1 (0) if the strings w1 and w2 are the same. Example: >> a = 'word'; >> b = ['wo', 'rd']; >> strcmp(a, b) ans = 1 >> a == b ans = 1 1 1 1
4
String operations strcat (w1, w2) concatenates the strings w1 and w2. Example: >> a = ‘this ’, b = ‘word'; >> c = strcat(a, b); >> c C = ‘thisword’ Strcat removes trailing blanks in the first argument. To avoid this removal, we can use: > c = [a, b];
5
In general, arrays can be merged using [ ] operation:
6
String operations A string is stored as a vector of (ASCII) characters. Example: >> str = 'abcdxyz'; >> str+0 ans = 97 98 99 100 120 121 122 ASCII for ‘a’ is 97, for z it is 97 + 25 = 122.
7
Collections of numbers and plotting Two ways to create a vector:
8
Creating a table of x and y values for plotting A common task is to create the data showing how a variable Y (temp) changes with another variable X (time). Example:
9
Plot of time vs. temp
10
Plotting graphs by mathematical relation
12
Boolean expressions Relational operators:, >=, ~=, == etc. Boolean connectives: & (and) | (or) ~ (not) xor (exclusive or) all any 1 represents TRUE, 0 represents FALSE
13
Boolean expressions Exercise: Write a one-line statement in MATLAB that returns the number of non- negative members in an array A.
14
Boolean expressions Exercise: Write a one-line statement in MATLAB that returns the number of non- negative members in an array A. Answer: sum (c >= 0)
15
Chapter 3 – solutions to some exercises and discussions Dicussion 3.1:
17
Exercise 3.1: sum(2.^(0 : 4)) Exercise 3.5:
18
Exercise 3.12:
20
Exercise 3.12: How many leap years are between 1780 and 2832, inclusive? Recall the definition of a leap year. Exercise 3.8. Write the MATLAB version of the boolean statement a <= b <= c.
21
Solution to Exercise 3.12:
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.