[translation] boolean expressions, some typos fixes

This commit is contained in:
Jan Grewe 2016-10-12 11:22:51 +02:00
parent b08ff66959
commit 11240e18c0

View File

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