diff --git a/programming/lecture/programming.tex b/programming/lecture/programming.tex index bc4e4b4..4e570d1 100644 --- a/programming/lecture/programming.tex +++ b/programming/lecture/programming.tex @@ -833,11 +833,11 @@ the statements but not both are true. There is no operator for XOR in Table~\ref{logicalrelationaloperators} show the logical and relational operators that are available in \matlab{}. The additional -\code[Operator!logical!and2@\&\&]{\&\&} und +\code[Operator!logical!and2@\&\&]{\&\&} and \code[Operator!logical!or2@{"|}{"|} {}]{||} operators are the so -called `\enterm{shorrt-circuit} operators for the logical OR and -AND. Short-circuit means that \matlab{} stopps to evaluate a Boolean -expresssion as soon as it becomes clear that the whole expression +called `\enterm{short-circuit} operators for the logical OR and +AND. Short-circuit means that \matlab{} stops to evaluate a Boolean +expression as soon as it becomes clear that the whole expression cannot become true. For example assume that the two statements A and B are joined using a AND. The whole expression can only be true if A is already true. This means, that there is no need to evaluate B if A is @@ -865,7 +865,7 @@ this saves processing time. \varcode{$\sim=$} & unequal\\ \varcode{$>$} & greater than \\ \varcode{$<$} & less than \\ - \varcode{$>=$} & greateror equal \\ + \varcode{$>=$} & greater or equal \\ \varcode{$<=$} & less or equal \\ \hline \end{tabular} @@ -881,12 +881,12 @@ this saves processing time. Previously we have introduced the data types for integer or floating point numbers and discussed that there are instances where it is more efficient to use a integer data type rather than storing floating -poing numbers. The result of a Boolean expression can only assume two +point numbers. The result of a Boolean expression can only assume two values (true or false). This implies that we need only a single bit to store this information as a 0 (false) and 1 (true). In \matlab{} knows a special data type (\codeterm{logical}) to store the result of a -Boolean expresssion. Every variable can be evaluated to true or false -by just onverting it to the logical data type. When doing so \matlab{} +Boolean expression. Every variable can be evaluated to true or false +by converting it to the logical data type. When doing so \matlab{} interprets all values different form zero to be true. In listing~\ref{booleanexpressions} we show several examples for such operations. \matlab{} also knows the keywords \code{true} and @@ -906,6 +906,8 @@ ans = 1 ans = 0 >> logical('test') ans = 1 1 1 1 +>> logical([1 2 3 4 0 0 10]) +and = 1 1 1 1 0 0 1 >> 1 > 2 ans = 0 >> 1 < 2 @@ -919,83 +921,85 @@ ans = 1 0 1 1 0 \end{lstlisting} -\section{Logisches Indizieren}\label{logicalindexingsec} - -Einer der wichtigsten Einsatzorte f\"ur boolesche Ausdr\"ucke ist das -logische Indizieren. Logisches Indizieren ist eines der -Schl\"usselkonzepte in \matlab{}. Nur mit diesem k\"onnen -Filteroperationen auf Vektoren und Matrizen effizient durchgef\"uhrt -werden. Es ist sehr m\"achtig und, wenn es einmal verstanden wurde, -sehr intuitiv zu benuzten. - -Das Grundkonzept hinter der logischen Indizierung ist, dass durch die -Verwendung eines booleschen Ausdrucks auf z.B. einen Vektor ein -logischer Vektor gleicher Gr\"o{\ss}e zur\"uckgegeben wird. Dieser -wird benutzt um die Elemente des urspr\"unglichen Vektors -auszuw\"ahlen, bei denen der logische Vektor \codeterm{wahr} ist -(Listing \ref{logicalindexing1}). Zeile 14 kann wie -folgt gelesen werden: Gib die Elemente von \varcode{x} an den -Stellen, an denen \varcode{x < 0} wahr ist, zur\"uck. - -\begin{lstlisting}[caption={Beispiel logisches Indizieren.}, label=logicalindexing1] ->> x = randn(1, 6) % Zeilenvektor mit 6 Zufallszahlen +\section{Logical indexing}\label{logicalindexingsec} +We have introduced how one can select certain element of a vector or +matrix by addressing the respective elements by their index. This is +fine when we know the range of elements we want t select. There are, +however, many situations in which a selection based on the value of +the stored element is desired. These situations is one of the major +places where we need Boolean expressions. The selection based on the +result of a Boolean expression is called \enterm{logical + indexing}. With this approach we can easily filter based on the +values stored in a vector or matrix. It is very powerful and, once +understood, very intuitive. + +The basic concept is that applying a Boolean operation on a vector +results in a \code{logical} vector of the same size (see +listing~\ref{booleanexpressions}. This logical vector is then used to +select only those values for which the logical vector is true. Line 14 +in listing~\ref{logicalindexing} can be read: ``Give me all those +elements of \varcode{x} where the Boolean expression \varcode{x < 0} +evaluates to true''. + +\begin{lstlisting}[caption={Logical indexing.}, label=logicalindexing1] +>> x = randn(1, 6) % a vector with 6 random numbers x = -1.4023 -1.4224 0.4882 -0.1774 -0.1961 1.4193 ->> % logisches Indizieren in zwei Schritten: ->> x_smaller_zero = x < 0 % logischer Vektor +>> % logical indexing in two steps +>> x_smaller_zero = x < 0 % create the logical vector x_smaller_zero = 1 1 0 1 1 0 ->> elements_smaller_zero = x(x_smaller_zero) % benutzen, um zuzugreifen +>> elements_smaller_zero = x(x_smaller_zero) % use it to select elements_smaller_zero = -1.4023 -1.4224 -0.1774 -0.1961 ->> % logisches Indizieren in einem Schritten: +>> % logical indexing with a single command >> elements_smaller_zero = x(x < 0) elements_smaller_zero = -1.4023 -1.4224 -0.1774 -0.1961 \end{lstlisting} \begin{exercise}{logicalVector.m}{logicalVector.out} - Erstelle einen Vektor \varcode{x} mit den Werten 0--10. + Create a vector \varcode{x} containing the values 0--10. \begin{enumerate} - \item F\"uhre aus: \varcode{y = x < 5} - \item Gib den Inhalt von \varcode{y} auf dem Bildschirm aus. - \item Was ist der Datentyp von \varcode{y}? - \item Gibt alle Elemente aus \varcode{x} zur\"uck, die kleiner als 5 sind. + \item Execute: \varcode{y = x < 5} + \item Display the content of \varcode{y} in the command window. + \item What is the data type of \varcode{y}? + \item Return only those elements \varcode{x} that are less than 5. \end{enumerate} \pagebreak[4] \end{exercise} \begin{figure}[t] \includegraphics[width= 0.9\columnwidth]{logicalIndexingTime} - \titlecaption{Beispiel f\"ur logisches Indizieren.} - {Der rot markierte Abschnitt aus den Daten wurde indirekt - mit logischem Indizieren auf dem Zeitvektor - ausgew\"ahlt (\varcode{x(t > 5 \& t < 6)}).}\label{logicalindexingfig} + \titlecaption{Example for logical indexing.} {The highlighted + segment of the data was selected using logical indexing on + the time vector: (\varcode{x(t > 5 \& t < + 6)}).}\label{logicalindexingfig} \end{figure} -Logisches Indizieren wurde oben so benutzt, dass die Auswahl auf dem -Inhalt desselben Vektors beruhte. Ein weiterer sehr h\"aufiger Fall -ist jedoch, dass die Auswahl aus einem Vektor auf dem Inhalt eines -zweiten Vektors basiert. Ein Beispiel ist, dass \"uber einen -gewissen Zeitraum Daten aufgenommen werden und aus diesen die Daten eines -bestimmten Zeitraums ausgew\"ahlt werden sollen (\figref{logicalindexingfig}). +So far we have used logical indexing to select elements of a vector +that match a certain condition. When analyzing data we are often +faced with the problem that we want to select the elements of one +vector for the case that the elements of a second vector assume a +certain value. One example for such a use-case is the selection of a +segment of data of a certain time span (the stimulus was on, +\figref{logicalindexingfig}). \begin{exercise}{logicalIndexingTime.m}{} - Angenommen es werden \"uber einen bestimmten Zeitraum Messwerte - genommen. Bei solchen Messungen erh\"alt man einen Vektor, der die - Zeitpunkte der Messung speichert und einen zweiten mit den - jeweiligen Messwerten. + Assume that measurements have been made for a certain time. Usually + measured values and the time are stored in two vectors. \begin{itemize} - \item Erstelle einen Vektor \varcode{t = 0:0.001:10;}, der die Zeit - repr\"asentiert. - \item Erstelle einen zweiten Vektor \varcode{x} mit Zufallszahlen, der - die gleiche L\"ange hat wie \varcode{t}. Die Werte darin stellen - Messungen zu den Zeitpunkten in \varcode{t} dar. - \item Benutze logische Indizieren um die Messwerte - auszuw\"ahlen, die dem zeitlichen Abschnitt 5--6\,s entsprechen. + \item Create a vector that represents the recording time \varcode{t + = 0:0.001:10;}. + \item Create a second vector \varcode{x} filled with random number + that has the same length as \varcode{t}. The values stored in + \varcode{x} represent the measured data at the times in + \varcode{t}. + \item Use logical indexing to select those values that have been + recorded in the time span form 5--6\,s. \end{itemize} \end{exercise}