Tutorials:

Logical expressions are the starting point for the 'loops' in your script, and any other automatic computations in computers. In short, before the computer decides to do anything, the logic operation will provide important information for the judgement to be made.

Read Gilat 6.1-6.2

Work these problems:

Task 1:

The monthly precipitation (in.) for Boston and Seattle during 2012 are given in the vectors below:

BOS = [2.67 1.00 1.21 3.09 3.43 4.71 3.88 3.08 4.10 2.62 1.01 5.93]

SEA = [6.83 3.63 7.20 2.68 2.05 2.96 1.04 0.00 0.03 6.71 8.28 6.85]

where the elements in the vectors are in the order of the months (Jan, Feb, etc.).

a) Calculate the total precipitation for the year, and monthly average precipitation for each city

b) How many months was the precipitation above the average in each city?

c) How many months was the precipitation in Boston lower than the precipitation in Seattle? Work this part using logical arrays.

d) Repeat question c using the find function instead of logical arrays. HINT: the length function may be handy.

Display formatted results, e.g.

Boston total precip and monthly average (in.): x.xx x.xx

Seattle total precip and monthly average (in.): x.xx x.xx

Boston number of months above average: x

Seattle number of months above average: x

Boston number of months with lower precip: x

Boston number of months with lower precip: x

Task 2:

Write a script which

a. Generates a row vector of 20 integer elements with values between -10 and 10.

b. Replaces all the negative elements with positive elements

data=linspace(-10,10,20)

neg = data<0;

data(neg) = -data(neg)

%%% Please note that:

The matrix 'neg' is created by data<0, which is a 'logical array', but not a simple data matrix:

Try this:

data=[2 3;4 5]

index=data>3

You will get:

index =

2×2 logical array

0 0

1 1

Note that they are 'logical array'.

Then do:

>> data(index)

ans =

4

5

The logical matrix can extract the data from a certain place, and put them in a column.

In the task above, you have:

data(neg) = -data(neg)

'neg' is a logical matrix, the command extract the corresponding values and put them in a column, and then replaced the corresponding values in itself.

Send your work to the email by Fri.