Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 3 August 31 Chapter 3. Chapter 3 – numbers, string, booleans integer: MATLAB stores numeric data as double-precision floating point (double) by.

Similar presentations


Presentation on theme: "Lecture 3 August 31 Chapter 3. Chapter 3 – numbers, string, booleans integer: MATLAB stores numeric data as double-precision floating point (double) by."— Presentation transcript:

1 Lecture 3 August 31 Chapter 3

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

3 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

4 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 >>

5 Complex numbers

6 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’

7 String operations >> length(word) ans = 5 >> strcmp(word, ‘hello!’) ans = 0 1 if the Boolean expression is true, 0 else. strcmp compares two strings.

8 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

9 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

10 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];

11 In general, arrays can be merged using [ ] operation:

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

13 Collections of numbers and plotting Two ways to create a vector:

14 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:

15 Plot of time vs. temp

16 Plotting graphs by mathematical relation

17

18 Boolean expressions Relational operators:, >=, ~=, == etc. Boolean connectives: & (and) | (or) ~ (not) xor (exclusive or) all any 1 represents TRUE, 0 represents FALSE

19 Boolean expressions Exercise: Write a one-line statement in MATLAB that returns the number of non- negative members in an array A.

20 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)

21 Chapter 3 – solutions to some exercises and discussions Dicussion 3.1:

22

23 Exercise 3.1: sum(2.^(0 : 4)) Exercise 3.5:

24 Exercise 3.6 Write a code segment in Matlab to draw the rectangle shown in the figure below:

25

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

27 Solution to Exercise 3.8: Solution to Exercise 3.12: (a >= b) && (b >= c)


Download ppt "Lecture 3 August 31 Chapter 3. Chapter 3 – numbers, string, booleans integer: MATLAB stores numeric data as double-precision floating point (double) by."

Similar presentations


Ads by Google