\documentclass[12pt,a4paper,pdftex]{exam} \usepackage[german]{babel} \usepackage{natbib} \usepackage{graphicx} \usepackage[small]{caption} \usepackage{sidecap} \usepackage{pslatex} \usepackage{amsmath} \usepackage{amssymb} \setlength{\marginparwidth}{2cm} \usepackage[breaklinks=true,bookmarks=true,bookmarksopen=true,pdfpagemode=UseNone,pdfstartview=FitH,colorlinks=true,citecolor=blue]{hyperref} %%%%% text size %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \usepackage[left=20mm,right=20mm,top=25mm,bottom=25mm]{geometry} \pagestyle{headandfoot} \header{{\bfseries\large Exercise 4 }}{{\bfseries\large Boolean Expressions and logical indexing}}{{\bfseries\large 23. Oktober, 2018}} \firstpagefooter{Dr. Jan Grewe}{Phone: 29 74588}{Email: jan.grewe@uni-tuebingen.de} \runningfooter{}{\thepage}{} \setlength{\baselineskip}{15pt} \setlength{\parindent}{0.0cm} \setlength{\parskip}{0.3cm} \renewcommand{\baselinestretch}{1.15} \newcommand{\code}[1]{\texttt{#1}} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \begin{document} \vspace*{-6.5ex} \begin{center} \textbf{\Large Introduction to scientific computing}\\[1ex] {\large Jan Grewe, Jan Benda}\\[-3ex] Abteilung Neuroethologie \hfill --- \hfill Institut f\"ur Neurobiologie \hfill --- \hfill \includegraphics[width=0.28\textwidth]{UT_WBMW_Black_RGB} \\ \end{center} 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+ \part \verb+y < x+ \part \verb+x == y+ \part \verb+x ~= y+ \part \verb+x & ~y+ \part \verb+x | y+ \end{parts} \question Find out what the functions \verb+bitand+ and \verb+bitor+ do. \begin{parts} \part Execute and explain: \verb+bitand(10, 8)+ \part Execute and explain: \verb+bitor(10, 8)+ \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. \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. \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}