270 likes | 344 Vues
Lecture 3 August 31 Chapter 3. Chapter 3 – numbers, string, booleans integer:
E N D
Lecture 3 August 31 • Chapter 3
Chapter 3 – numbers, string, booleans integer: MATLAB stores numeric data as double-precision floating point (double) by default. To store data as an integer, you need to convert from double to the desired integer type. Example: To store 325 as a 16-bit signed integer assigned to variable x: >> x = int16(325); If the number being converted to an integer has a fractional part, MATLAB rounds to the nearest integer.
If the fractional part is exactly 0.5, then from the two equally nearby integers, MATLAB chooses the one for which the absolute value is larger in magnitude: • >> x = 325.499; • >> int16(x) • ans = 325 • >> x = x + .001; • >> int16(x) • ans = 326 Built-in functions that convert to int • Other related functions: floor, ceil
long floating-point format >> format long >> x = 1.5^2.3; >> x x = 2.54103060477792 >> format short >> x x = 2.5410 >> x = 2.564593653; >> x x = 2.5646 >>
Strings Character: alphabetical – upper/lower (‘a’ .. ‘z’, ‘A’ .. ‘Z’) digits – 0, 1, …, 9 special characters - $, % etc. control characters - \n (end of line) etc. Each character is encoded by 8 bits (ASCII) or 16 bits (unicode) Unicode allows encoding of alphabets from many languages such as Hungarian, Chinese, Swahili etc. String - sequence of characters. >> greeting = ‘hello’
String operations >> length(word) ans = 5 >> strcmp(word, ‘hello!’) ans = 0 1 if the Boolean expression is true, 0 else. strcmp compares two strings.
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
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
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];
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.
Collections of numbers and plotting Two ways to create a vector:
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:
Boolean expressions Relational operators: < , > , >= , ~= , == etc. Boolean connectives: & (and) | (or) ~ (not) xor (exclusive or) all any 1 represents TRUE, 0 represents FALSE
Boolean expressions Exercise: Write a one-line statement in MATLAB that returns the number of non-negative members in an array A.
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)
Chapter 3 – solutions to some exercises and discussions Dicussion 3.1:
Exercise 3.1: sum(2.^(0 : 4)) Exercise 3.5:
Exercise 3.6 Write a code segment in Matlab to draw the rectangle shown in the figure below:
Exercise 3.8. Write the MATLAB version of the boolean statement a <= b <= c. Exercise 3.12: How many leap years are between 1780 and 2832, inclusive? Recall the definition of a leap year.
Solution to Exercise 3.8: Solution to Exercise 3.12: (a >= b) && (b >= c)