Second lecture

Contents

Logical variables

We can test the equality of two variables with the == operator

1==2;

the answer is a variable, which class is logical

whos
  Name      Size            Bytes  Class      Attributes

  ans       1x1                 1  logical              

We have to be careful, when comparing floating point numbers. For example:

1==1+1e-15
ans =

  logical

   0

but

1==1+1e-16
ans =

  logical

   1

The other comparing operators are: ~= (not equal), <, >, <=, >=

Automatic conversion of logical variables: In fact, in Matlab every floating point number, which doesn't equal to 0 is a true logical variable, and 0 as a floating point variable is a false logical variable. Hence:

 1>(0==2)
ans =

  logical

   1

Comparing vectors

We can also compare the (same length!) vectors

v=[1 2 5];
w=[1 3 5];

v==w
ans =

  1×3 logical array

   1   0   1

v and w don't have to be same sized, just same length

v==w'
ans =

  3×3 logical array

   1   0   0
   0   0   0
   0   0   1

Combining conditions

AND: "&&" . Both sides have to be scalars, not vectors

(5<6) && (6>5)
ans =

  logical

   1

OR: "||"

(5<6) || (6<5)
ans =

  logical

   1

For now, use () for the conditions, later we will learn about precedence.

Indexing with logical variables

The element of a vector can be indexed not only with their positions, but also with their properties. For example

v=[0 1 2 3 4 5 6 7 8 9];
w=[0 2 1 3 4 6 5 7 9 8];
v==w
ans =

  1×10 logical array

   1   0   0   1   1   0   0   1   0   0

These are the elements of v, which are in the same position in w

v(v==w)
ans =

     0     3     4     7

It's possible, to write any condition inside, for example:

v(v>5)
ans =

     6     7     8     9

We can change these elements

v(v>5)=0
v =

     0     1     2     3     4     5     0     0     0     0

or multiply them by 5
v(v<4)=5*v(v<4)
v =

     0     5    10    15     4     5     0     0     0     0

For indexing we can combine vectors with the & (elementwise and) and | (elementwise or) operators.

v(v>=3 & v<=6)
ans =

     5     4     5

Conditional statments

An example:

x=0.1
if x==0.1
    fprintf('Really, x=%2.1f\n',x)
elseif x<=0 && x>0
    fprintf('We will never see this line\n')
elseif x<=0 || x>0
    fprintf('Because if x~=0.1 we see this\n')
else
    fprintf('Hence, we never get here\n')
end
x =

   0.100000000000000

Really, x=0.1

Only the if + condition + command + end part is obligatory, the else and elseif (there can be more of them) branches are optional. Vectors are not recommended as condition, since the branch will be executed only once, and only if all of the elements are true.

Recommended problems

1: Write a function called nearEnough, which takes two floating point numbers as input. The output is 1, if they distance is less then 0.001, 0 otherwise.

2. Write a function called numInt, which takes a vector as input. The output is the number of integers amongst the elements of v.

3. Write a function called nDiff, which takes a vector as input. The output is a vector, which elements are the differences between the consecutive elements of v.

4. Write a function called midTerm, which takes a integer between 0 and 100 as input. The output is your midterm mark, if the input is the sum of point you collected during the semester.

Optional homework

5. Write a function called modif, which takes a vector as input. The output vector is the same as the input except all negative or greater than 10 elements are removed, and the rest of the elements are doubled.

6. Write a function called beforeAfter, which takes four numbers as input. These four number are the dates of two events: the first one's year and month, and the second one's year and month. Your function have to decide which one happened first, and print this on the screen. The function should not have an output.