\documentclass[12pt,a4paper,pdftex]{exam}

\newcommand{\exercisetopic}{Logical indexing}
\newcommand{\exercisenum}{4}
\newcommand{\exercisedate}{29. Oktober, 2019}

\input{../../exercisesheader}

\firstpagefooter{Dr. Jan Grewe}{}{jan.grewe@uni-tuebingen.de}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}

\input{../../exercisestitle}

The exercises are meant for self-monitoring and revision of the
lecture. You should try to solve them on your own. Your solution
should be submitted as a single script (m-file) in the Ilias
system. Each task should be solved in its own ``cell''. Each cell must
be executable on its own. The file should be named according to the
following pattern: ``variables\_datatypes\_\{lastname\}.m''
(e.g. variables\_datentypes\_mueller.m).

\section{Boolean expressions}

\begin{questions}
  \question Consider the following vectors \verb+x = [1 5 2 8 9 0 1]+ and
  \verb+y = [5 2 2 6 0 0 2]+. Execute the following commands and explain.
  \begin{parts}
    \part \verb+x > y+
    \begin{solution}
      Returns a vector of zeros and ones. The vector is one for each
      position at which the value of x is larger than the
      respective counterpart in y.
    \end{solution}
    \part \verb+y < x+
    \begin{solution}
      same result, because we are inverting the expression.
    \end{solution}
    \part \verb+x == y+
    \begin{solution}
      only 1 for same values in x and y
    \end{solution}
    \part \verb+x ~= y+
    \begin{solution}
      1 for all indices where x and y values are not the same.
    \end{solution}
    \part \verb+x & ~y+
    \begin{solution}
      1 only for the indices in which x is ~= 0 AND y is 0.
      Implicit conversion of the values in x and y to logicals.
    \end{solution}
    \part \verb+x | y+
    \begin{solution}
      1 at the indices where x or y are ~= 0.
    \end{solution}
  \end{parts}

  \question What is the difference between the logical operators
  \verb+&+ and \verb+&&+ and \verb+|+ and \verb+||+ and why are they
  useful?
  \begin{solution}
    \verb+&&+ and \verb+||+ are called short-ciruit AND and OR. In
    case of an AND expression, the right-hand side is only evaluated
    if the left-hand side yields \verb+true+. Similarly for OR, the
    right-hand side is only evaluated if the left-hand side yields
    \verb+false+.

    Saves processing time.
  \end{solution}
  
  \question Find out what the functions \verb+bitand+ and \verb+bitor+ do.
  \begin{parts}
    \part Execute and explain: \verb+bitand(10, 8)+
    \begin{solution}
      compares the binary patterns of both numbers with a logican AND
      and returns the decimal value of the resulting binary pattern.
      The only bit that is ``on'' for both numbers is the '8', the
      result is thus '8'.
    \end{solution}
    \part Execute and explain: \verb+bitor(10, 8)+
    \begin{solution}
      Comparison with a logical OR. 8 and 2 bits are ``on'' in at
      least one of the two binary patterns, the result will be 10.
    \end{solution}
  \end{parts}
\item Implement the following Boolean expressions. Test your
  implementations using random integer
  numbers for \verb+x+ and \verb+y+ (\verb+randi+).
  \begin{parts}
    \part The result should be \verb+true+ if \verb+x+ is greater than \verb+y+ and the sum of \verb+x+ and \verb+y+ is not less than 100.
    \begin{solution}
      \code{x > y \&  (x+y) >= 100}
    \end{solution}
    \part The result should be \verb+true+ if \verb+x+ and \verb+y+ are not equal zero or \verb+x+ and \verb+y+ are equal.
    \begin{solution}
      \code{(x ~= 0  \&  y ~= 0) | x == y}
    \end{solution}

  \end{parts}
\end{questions}

\newpage
\section{Logical Indexing}

Boolean expressions can be used to select elements of vectors or
matrices that match in certain criteria. This process is called
\emph{logical indexing}.

\begin{questions}
  \question Given are the vectors \verb+x = (1:10)+ and
  \verb+y = [3 1 5 6 8 2 9 4 7 0]+. Try to understand the results of
  the following commands and explain.
  \begin{parts}
    \part \verb+x < 5+
    \part \verb+x( (x < 5) )+
    \part \verb+x( (y <= 2) )+
    \part \verb+x( (x > 2) | (y < 8) )+
    \part \verb+x( (x == 0) & (y == 0) )+
  \end{parts}

  \question Test the random number generator:
  \begin{parts}
    \part Create a $100 \times 100$ 2-D matrix that is filled with random numbers in the range 0 to 100. Replace the elements according to these rules: \verb+x < 33+ to 0,
    \verb+x >= 33 and x < 66+ to 1 and all \verb+x >= 66+ to 2.
    \part Count the number of elements in each class using Boolean expressions (\verb+sum+ can be used to count the matches).
  \end{parts}


  \question Plotting a periodic signal. 
  \begin{parts}
    \part Load the file ``signal.mat'' into the workspace (use
    \verb+load('signal.mat')+ or use the UI for it). It contains two
    variables \verb+signal+ and \verb+time+. The signal has a period of
    0.2\,s
    \part What is the size of the data?
    \part What is the temporal resolution of the time axis?
    \part Plot the full data. Make sure that the axes are properly labeled (script chapter 3, or the matlab documentation).
    \part Use logical indexing to select the periods individually and plot them into the same plot (first period starts at time 0.0\,s, the next at 0.2\,s and so on).
  \end{parts}
\end{questions}

\end{document}
