Lecture 3 August 31

27
Lecture 3 August 31 • Chapter 3

description

Lecture 3 August 31 Chapter 3. Chapter 3 – numbers, string, booleans integer: - PowerPoint PPT Presentation

Transcript of Lecture 3 August 31

Page 1: Lecture 3                                                                         August 31

Lecture 3

August 31

• Chapter 3

Page 2: Lecture 3                                                                         August 31

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.

Page 3: Lecture 3                                                                         August 31

• 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

Page 4: Lecture 3                                                                         August 31

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

>>

Page 5: Lecture 3                                                                         August 31

Complex numbers

Page 6: Lecture 3                                                                         August 31

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’

Page 7: Lecture 3                                                                         August 31

String operations

>> length(word)

ans = 5

>> strcmp(word, ‘hello!’)

ans = 0

1 if the Boolean expression is true, 0 else.

strcmp compares two strings.

Page 8: Lecture 3                                                                         August 31

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

Page 9: Lecture 3                                                                         August 31

String operationsstrcmp(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

Page 10: Lecture 3                                                                         August 31

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

Page 11: Lecture 3                                                                         August 31

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

Page 12: Lecture 3                                                                         August 31

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.

Page 13: Lecture 3                                                                         August 31

Collections of numbers and plotting

Two ways to create a vector:

Page 14: Lecture 3                                                                         August 31

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:

Page 15: Lecture 3                                                                         August 31

Plot of time vs. temp

Page 16: Lecture 3                                                                         August 31

Plotting graphs by mathematical relation

Page 17: Lecture 3                                                                         August 31
Page 18: Lecture 3                                                                         August 31

Boolean expressions

Relational operators:

< , > , >= , ~= , == etc.

Boolean connectives:

& (and) | (or) ~ (not) xor (exclusive or) all any

1 represents TRUE, 0 represents

FALSE

Page 19: Lecture 3                                                                         August 31

Boolean expressions

Exercise: Write a one-line statement in

MATLAB that returns the number of non-

negative members in an array A.

Page 20: Lecture 3                                                                         August 31

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)

Page 21: Lecture 3                                                                         August 31

Chapter 3 – solutions to some exercises and discussions

Dicussion 3.1:

Page 22: Lecture 3                                                                         August 31
Page 23: Lecture 3                                                                         August 31

Exercise 3.1:

sum(2.^(0 : 4))

Exercise 3.5:

Page 24: Lecture 3                                                                         August 31

Exercise 3.6

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

Page 25: Lecture 3                                                                         August 31
Page 26: Lecture 3                                                                         August 31

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.

Page 27: Lecture 3                                                                         August 31

Solution to Exercise 3.8:

Solution to Exercise 3.12:

(a >= b) && (b >= c)