some more language fixes
This commit is contained in:
parent
6204a45f23
commit
8be04a9b06
@ -6,18 +6,17 @@
|
|||||||
|
|
||||||
\subsection{Variables}
|
\subsection{Variables}
|
||||||
|
|
||||||
A \enterm{variable} is a pointer to a certain place in the computer's
|
A \enterm{variable} is a pointer to a certain address in the
|
||||||
memory. This pointer is characterized by its name, the variable's
|
computer's memory. This pointer is characterized by it's name and the
|
||||||
name, and the \enterm{data type} (figure~\ref{variablefig}). In the
|
\enterm{data type} (figure~\ref{variablefig}). In the computer's
|
||||||
computer's memory the value of the variable is stored in binary form
|
memory the value of the variable is stored in binary form, that is, as
|
||||||
that is as a sequence of zeros and ones (\enterm[Bit]{Bits}). When the
|
a sequence of zeros and ones (\enterm[Bit]{Bits}). When the variable
|
||||||
variable is read from the memory, this binary pattern is interpreted
|
is read from the memory, this binary pattern is interpreted according
|
||||||
according to the data type. The example shown in
|
to the data type. The example shown in figure~\ref{variablefig} shows
|
||||||
figure~\ref{variablefig} shows that the very same bit pattern is either
|
that the very same bit pattern is either interpreted as an 8-bit
|
||||||
interpreted as a 8-bit integer type (numeric value 38) or as a
|
integer type (numeric value 38) or as the ampersand (\&) character. In
|
||||||
ampersand (\&) character. In \matlab{} data types are of only minor
|
\matlab{} data types are of only minor importance but there are
|
||||||
importance but there are occasions where it becomes important to know
|
occasions in which it becomes important to know the type of a variable.
|
||||||
the type of a variable and we will come back to them later on.
|
|
||||||
|
|
||||||
\begin{figure}
|
\begin{figure}
|
||||||
\centering
|
\centering
|
||||||
@ -29,7 +28,7 @@ the type of a variable and we will come back to them later on.
|
|||||||
\includegraphics[width=.8\textwidth]{variableB}
|
\includegraphics[width=.8\textwidth]{variableB}
|
||||||
\label{variable:b}
|
\label{variable:b}
|
||||||
\end{subfigure}
|
\end{subfigure}
|
||||||
\titlecaption{Variables.}{Variables are point to a memory
|
\titlecaption{Variables.}{Variables point to a memory
|
||||||
address. They further are described by their name and
|
address. They further are described by their name and
|
||||||
data type. The variable's value is stored as a pattern of binary
|
data type. The variable's value is stored as a pattern of binary
|
||||||
values (0 or 1). When reading the variable this pattern is
|
values (0 or 1). When reading the variable this pattern is
|
||||||
@ -56,8 +55,8 @@ z =
|
|||||||
A
|
A
|
||||||
\end{lstlisting}
|
\end{lstlisting}
|
||||||
|
|
||||||
Line 1 can be read like: ``create a variable with the name \varcode{x}
|
Line 1 can be read like: ``Create a variable with the name \varcode{x}
|
||||||
and assign the value 38''. The equal sign is the so called
|
and assign the value 38''. The equality sign is the so called
|
||||||
\codeterm{assignment operator}. Line 5 defines a variable \varcode{y}
|
\codeterm{assignment operator}. Line 5 defines a variable \varcode{y}
|
||||||
and assigns an empty value. If not explicitly specified \matlab{}
|
and assigns an empty value. If not explicitly specified \matlab{}
|
||||||
variables will have the \codeterm{double} (a numeric data type, see
|
variables will have the \codeterm{double} (a numeric data type, see
|
||||||
@ -66,10 +65,12 @@ and assign the character ``A'' to it. Accordingly, \varcode{z} does
|
|||||||
not have the numeric \codeterm{double} data type but is of the type
|
not have the numeric \codeterm{double} data type but is of the type
|
||||||
\codeterm{character}.
|
\codeterm{character}.
|
||||||
|
|
||||||
The actual data type of a variable can be found out with the
|
There are two ways to find out the actual data type of a variable: the
|
||||||
\code{class()} function. \code{who} prints a list of all defined
|
\code{class()} and the \code{whos} functions. While \code{class()}
|
||||||
variables and \code{whos} provides detailed information
|
simply returns the data type, \code{whos} offers more detailed
|
||||||
(listing~\ref{varListing2}).
|
information but it is not suited to be used in programs (see
|
||||||
|
also the \code{who} function that returns a list of all defined
|
||||||
|
variables, listing~\ref{varListing2}).
|
||||||
|
|
||||||
\begin{lstlisting}[label=varListing2, caption={Requesting information about defined variables and their types.}]
|
\begin{lstlisting}[label=varListing2, caption={Requesting information about defined variables and their types.}]
|
||||||
>>class(x)
|
>>class(x)
|
||||||
@ -90,10 +91,11 @@ x y z
|
|||||||
\end{lstlisting}
|
\end{lstlisting}
|
||||||
|
|
||||||
\begin{important}[Naming conventions]
|
\begin{important}[Naming conventions]
|
||||||
There are a few rules regarding the variable names. \matlab{} is
|
There are a few rules regarding variable names. \matlab{} is
|
||||||
case-sensitive, i.e. \code{x} and \code{X} are two different
|
case-sensitive, i.e. \code{x} and \code{X} are two different
|
||||||
names. Names must begin with an alphabetic character. German (or
|
names. Names must begin with an alphabetic character. German (or
|
||||||
other) umlauts, special characters and spaces are forbidden.
|
other) umlauts, special characters and spaces are forbidden in
|
||||||
|
variable names.
|
||||||
\end{important}
|
\end{important}
|
||||||
|
|
||||||
\subsection{Working with variables}
|
\subsection{Working with variables}
|
||||||
@ -104,7 +106,7 @@ such as \code[Operator!arithmetic!1add@+]{+},
|
|||||||
\code[Operator!arithmetic!3mul@*]{*} and
|
\code[Operator!arithmetic!3mul@*]{*} and
|
||||||
\code[Operator!arithmetic!4div@/]{/}. The power is denoted by the
|
\code[Operator!arithmetic!4div@/]{/}. The power is denoted by the
|
||||||
\code[Operator!arithmetic!5pow@\^{}]{\^{}}. Listing~\ref{varListing3}
|
\code[Operator!arithmetic!5pow@\^{}]{\^{}}. Listing~\ref{varListing3}
|
||||||
show their use.
|
shows their use.
|
||||||
|
|
||||||
\pagebreak[4]
|
\pagebreak[4]
|
||||||
\begin{lstlisting}[label=varListing3, caption={Working with variables.}]
|
\begin{lstlisting}[label=varListing3, caption={Working with variables.}]
|
||||||
@ -133,10 +135,10 @@ z =
|
|||||||
>> clear z % deleting a variable
|
>> clear z % deleting a variable
|
||||||
\end{lstlisting}
|
\end{lstlisting}
|
||||||
|
|
||||||
Note: in lines 2 and 6 the values of the variables have been used
|
Note: in lines 2 and 10 the variables have been used without changing
|
||||||
without changing their values. Whenever the value of a variable should
|
their values. Whenever the value of a variable should change, the
|
||||||
change, the \code[Operator!Assignment!=]{=} operator has to be used
|
\code[Operator!Assignment!=]{=} operator has to be used (lines 14 and
|
||||||
(lines 14 and 18). Line 23, finally shows how to delete a variable.
|
18). Line 23, finally shows how to delete a variable.
|
||||||
|
|
||||||
\subsection{Data types}
|
\subsection{Data types}
|
||||||
|
|
||||||
@ -144,17 +146,19 @@ As mentioned above, the data type associated with a variable defines how the sto
|
|||||||
\begin{itemize}
|
\begin{itemize}
|
||||||
\item \codeterm{integer}: Integer numbers. There are several subtypes
|
\item \codeterm{integer}: Integer numbers. There are several subtypes
|
||||||
which, for most use-cases, can be ignored when working in \matlab{}.
|
which, for most use-cases, can be ignored when working in \matlab{}.
|
||||||
\item \codeterm{double}: Floating point numbers. In contrast to the
|
\item \codeterm{single} \codeterm{double}: Floating point numbers. In
|
||||||
real numbers that are represented with this data type the number of
|
contrast to the real numbers that are represented with this data
|
||||||
numeric values that can be represented is limited (countable?).
|
type the number of numeric values that can be represented is limited
|
||||||
|
(countable).
|
||||||
\item \codeterm{complex}: Complex numbers having a real and imaginary
|
\item \codeterm{complex}: Complex numbers having a real and imaginary
|
||||||
part.
|
part.
|
||||||
\item \codeterm{logical}: Boolean values that can be evaluated to
|
\item \codeterm{logical}: Boolean values that can be evaluated to
|
||||||
\code{true} or \code{false}.
|
\code{true} or \code{false}.
|
||||||
\item \codeterm{char}: ASCII characters.
|
\item \codeterm{char}: ASCII characters.
|
||||||
\end{itemize}
|
\end{itemize}
|
||||||
There is a variety of numeric data types that require different memory
|
There is a variety of numeric data types that require different
|
||||||
demands and ranges of representable values (table~\ref{dtypestab}).
|
amounts of memory and have different ranges of values that can be
|
||||||
|
represented (table~\ref{dtypestab}).
|
||||||
|
|
||||||
\begin{table}[t]
|
\begin{table}[t]
|
||||||
\centering
|
\centering
|
||||||
@ -162,8 +166,9 @@ demands and ranges of representable values (table~\ref{dtypestab}).
|
|||||||
\label{dtypestab}
|
\label{dtypestab}
|
||||||
\begin{tabular}{llcl}\hline
|
\begin{tabular}{llcl}\hline
|
||||||
Data type & memory demand & range & example \erh \\ \hline
|
Data type & memory demand & range & example \erh \\ \hline
|
||||||
\code{double} & 64 bit & $\approx -10^{308}$ to $\approx 10^{308}
|
\code{single} & 32 bit & $\approx -3.4^{38}$ to $\approx 3.4^{38}$ & Floating point numbers.\erb \\
|
||||||
$& Floating point numbers.\erb\\ \code{int} & 64 bit & $-2^{31}$
|
\code{double} & 64 bit & $\approx -10^{308}$ to $\approx 10^{308}$ &
|
||||||
|
Floating point numbers.\erb\\ \code{int} & 64 bit & $-2^{31}$
|
||||||
to $2^{31}-1$ & Integer values. \\ \code{int16} & 16 bit &
|
to $2^{31}-1$ & Integer values. \\ \code{int16} & 16 bit &
|
||||||
$-2^{15}$ to $2^{15}-1$ & Digitizes measurements. \\ \code{uint8}
|
$-2^{15}$ to $2^{15}-1$ & Digitizes measurements. \\ \code{uint8}
|
||||||
& 8 bit & $0$ bis $255$ & Digitized intensities of colors in
|
& 8 bit & $0$ bis $255$ & Digitized intensities of colors in
|
||||||
@ -177,7 +182,6 @@ in which different data types are better suited. Box~\ref{daqbox}
|
|||||||
exemplifies such a case.
|
exemplifies such a case.
|
||||||
|
|
||||||
\begin{ibox}[t]{\label{daqbox}Digitizing measurements}
|
\begin{ibox}[t]{\label{daqbox}Digitizing measurements}
|
||||||
|
|
||||||
Scenario: The electric activity (e.g. the membrane potential) of a
|
Scenario: The electric activity (e.g. the membrane potential) of a
|
||||||
nerve cell is recorded. The measurements are digitized and stored on
|
nerve cell is recorded. The measurements are digitized and stored on
|
||||||
the hard disk of a computer for later analysis. This is done using a
|
the hard disk of a computer for later analysis. This is done using a
|
||||||
@ -217,9 +221,10 @@ Vectors and matrices are the most important data structures in
|
|||||||
between theses structures, they are one- or multidimensional
|
between theses structures, they are one- or multidimensional
|
||||||
\enterm{arrays}. Such arrays are structures that can store multiple
|
\enterm{arrays}. Such arrays are structures that can store multiple
|
||||||
values of the same data type in a single variable. Due to \matlab{}'s
|
values of the same data type in a single variable. Due to \matlab{}'s
|
||||||
origin in the handling of mathematical problems, they have different
|
origin in the handling of mathematical problems, they are called
|
||||||
name but are internally the same. Vectors are 2-dimensional matrices
|
differently but are internally the same. Vectors are 2-dimensional
|
||||||
in which one dimension has the size 1 (a singleton dimension).
|
matrices in which one dimension has the size 1 (a singleton
|
||||||
|
dimension).
|
||||||
|
|
||||||
\subsection{Vectors}
|
\subsection{Vectors}
|
||||||
|
|
||||||
@ -227,7 +232,7 @@ In contrast to variables that store just a single value
|
|||||||
(\enterm{scalar}) a vector can store multiple values of the same data
|
(\enterm{scalar}) a vector can store multiple values of the same data
|
||||||
type (figure~\ref{vectorfig}). The variable \varcode{a} for example stores four integer values.
|
type (figure~\ref{vectorfig}). The variable \varcode{a} for example stores four integer values.
|
||||||
|
|
||||||
\begin{figure}
|
\begin{figure}[ht]
|
||||||
\includegraphics[width=0.8\columnwidth]{scalarArray}
|
\includegraphics[width=0.8\columnwidth]{scalarArray}
|
||||||
\titlecaption{Scalars and vectors.}{\textbf{A)} A scalar variable
|
\titlecaption{Scalars and vectors.}{\textbf{A)} A scalar variable
|
||||||
holds exactly on value. \textbf{B)} A vector can hold multiple
|
holds exactly on value. \textbf{B)} A vector can hold multiple
|
||||||
@ -243,7 +248,7 @@ step-sizes unequal to 1. Line 5 can be read like: ``Create a variable
|
|||||||
\varcode{b} and assign the values from 0 to 9 in increasing steps of
|
\varcode{b} and assign the values from 0 to 9 in increasing steps of
|
||||||
1.''. Line 9 reads: ``Create a variable \varcode{c} and assign the
|
1.''. Line 9 reads: ``Create a variable \varcode{c} and assign the
|
||||||
values from 0 to 10 in steps of 2''.
|
values from 0 to 10 in steps of 2''.
|
||||||
|
\pagebreak
|
||||||
\begin{lstlisting}[label=generatevectorslisting, caption={Creating simple row-vectors.}]
|
\begin{lstlisting}[label=generatevectorslisting, caption={Creating simple row-vectors.}]
|
||||||
>> a = [0 1 2 3 4 5 6 7 8 9] % Creating a row-vector
|
>> a = [0 1 2 3 4 5 6 7 8 9] % Creating a row-vector
|
||||||
a =
|
a =
|
||||||
@ -309,18 +314,19 @@ ans =
|
|||||||
|
|
||||||
|
|
||||||
\subsubsection{Accessing elements of a vector}
|
\subsubsection{Accessing elements of a vector}
|
||||||
\begin{figure}
|
|
||||||
\includegraphics[width=0.4\columnwidth]{arrayIndexing}
|
|
||||||
\titlecaption{Index.}{Each element of a vector can be addressed via
|
|
||||||
its index (small numbers) to access its content (large
|
|
||||||
numbers).}\label{vectorindexingfig}
|
|
||||||
\end{figure}
|
|
||||||
|
|
||||||
The content of a vector is accessed using the element's index
|
The content of a vector is accessed using the element's index
|
||||||
(figure~\ref{vectorindexingfig}). Each element has an individual
|
(figure~\ref{vectorindexingfig}). Each element has an individual
|
||||||
\codeterm{index} that ranges (int \matlab{}) from 1 to the number of
|
\codeterm{index} that ranges (int \matlab{}) from 1 to the number of
|
||||||
elements irrespective of the type of vector.
|
elements irrespective of the type of vector.
|
||||||
|
|
||||||
|
\begin{figure}[ht]
|
||||||
|
\includegraphics[width=0.4\columnwidth]{arrayIndexing}
|
||||||
|
\titlecaption{Index.}{Each element of a vector can be addressed via
|
||||||
|
its index (small numbers) to access its content (large
|
||||||
|
numbers).}\label{vectorindexingfig}
|
||||||
|
\end{figure}
|
||||||
|
|
||||||
\begin{important}[Indexing]
|
\begin{important}[Indexing]
|
||||||
Elements of a vector are accessed via their index. This process is
|
Elements of a vector are accessed via their index. This process is
|
||||||
called \codeterm{indexing}.
|
called \codeterm{indexing}.
|
||||||
@ -693,15 +699,15 @@ ans =
|
|||||||
4
|
4
|
||||||
\end{lstlisting}
|
\end{lstlisting}
|
||||||
|
|
||||||
\begin{ibox}[t]{\label{matrixmultiplication} The matrix-multiplication.}
|
\begin{ibox}[tp]{\label{matrixmultiplication} The matrix--multiplication.}
|
||||||
The matrix-multiplication from linear algebra is \textbf{not} an
|
The matrix--multiplication from linear algebra is \textbf{not} an
|
||||||
element-wise multiplication of each element in a matrix \varcode{A}
|
element--wise multiplication of each element in a matrix \varcode{A}
|
||||||
and the respective element from matrix \varcode{B}. It is something
|
and the respective element of matrix \varcode{B}. It is something
|
||||||
completely different. Confusing element-wise and
|
completely different. Confusing element--wise and
|
||||||
matrix-multiplication is one of the most common mistakes in
|
matrix--multiplication is one of the most common mistakes in
|
||||||
\matlab{}. \linebreak
|
\matlab{}. \linebreak
|
||||||
|
|
||||||
The matrix-multiplication is only possible if the number of columns
|
The matrix--multiplication is only possible if the number of columns
|
||||||
in the first matrix agrees with the number of rows in the other. More
|
in the first matrix agrees with the number of rows in the other. More
|
||||||
formal: $\mathbf{A}$ and $\mathbf{B}$ can be multiplied $(\mathbf{A}
|
formal: $\mathbf{A}$ and $\mathbf{B}$ can be multiplied $(\mathbf{A}
|
||||||
\cdot \mathbf{B})$, if $\mathbf{A}$ has the size $(m \times n)$ and
|
\cdot \mathbf{B})$, if $\mathbf{A}$ has the size $(m \times n)$ and
|
||||||
@ -781,30 +787,30 @@ ans =
|
|||||||
|
|
||||||
Boolean expressions are instructions that can be evaluated to
|
Boolean expressions are instructions that can be evaluated to
|
||||||
\varcode{true} or \varcode{false}. In the context of programming they
|
\varcode{true} or \varcode{false}. In the context of programming they
|
||||||
are used to test the relations accordingly the programming language
|
are used to test the relations between entities, i.e. are entities the
|
||||||
defines operators for such instructions. The following
|
same, greater or less than? Accordingly, programming languages defines
|
||||||
\codeterm{relational operators} are defined:
|
operators for such instructions. The following \codeterm{relational
|
||||||
(\code[Operator!relational!>]{>}, \code[Operator!relational!<]{<},
|
operators} are defined: (\code[Operator!relational!>]{>},
|
||||||
\code[Operator!relational!==]{==}, \code[Operator!relational!"~]{~},
|
\code[Operator!relational!<]{<}, \code[Operator!relational!==]{==},
|
||||||
greater than, less than, equal to, and not. Via so called
|
\code[Operator!relational!"~]{~}, greater than, less than, equal to,
|
||||||
\codeterm[Operator!logical]{logical operators} it is possible to join
|
and not. Using so called \codeterm[Operator!logical]{logical
|
||||||
single Boolean expressions (\code[Operator!logical!and1@\&]{\&},
|
operators} allows to join single Boolean expressions to more complex
|
||||||
|
constructs (\code[Operator!logical!and1@\&]{\&},
|
||||||
\code[Operator!logical!or1@{"|} {}]{|}, AND, OR). These expressions
|
\code[Operator!logical!or1@{"|} {}]{|}, AND, OR). These expressions
|
||||||
are important to control which parts of the code should be evaluated
|
are important e.g. to control which parts of the code should be
|
||||||
under a certain condition (conditional statements,
|
evaluated under a certain condition (conditional statements,
|
||||||
Section~\ref{controlstructsec}) but also for accessing only certain
|
Section~\ref{controlstructsec}) but also for accessing only certain
|
||||||
elements of a vector or matrix (logical indexing,
|
elements of a vector or matrix (logical indexing,
|
||||||
Section~\ref{logicalindexingsec}).
|
Section~\ref{logicalindexingsec}).
|
||||||
|
|
||||||
The truth tables (\ref{logicalandor}) are used to visualize the
|
Truth tables (\ref{logicalandor}) are used to visualize the results of
|
||||||
results of Boolean expressions. The statements A and B can be
|
Boolean expressions. A and B are statements that can be evaluated to
|
||||||
evaluated to True or False. When they are combined with a logical AND
|
True or False. When they are combined with a logical AND the whole
|
||||||
the expression is true only if both statements are true. The logical
|
expression is True only if both statements are True. The logical OR,
|
||||||
OR, on the other hand, requires that at least one of the statements is
|
on the other hand, requires that at least one of the statements is
|
||||||
true. The exclusive OR (XOR) is true only for cases in which one of
|
True. The exclusive OR (XOR) is True only for cases in which one of
|
||||||
the statements but not both are true. There is no operator for XOR in
|
the statements but not both are True. There is no operator for XOR in
|
||||||
\matlab{} it is realized via the function \code[xor()]{xor(A,
|
\matlab{} it is realized via the function \code[xor()]{xor(A, B)}.
|
||||||
B)}.
|
|
||||||
|
|
||||||
\begin{table}[tp]
|
\begin{table}[tp]
|
||||||
\titlecaption{Truth tables for logical AND, OR and XOR.}{}\label{logicalandor}
|
\titlecaption{Truth tables for logical AND, OR and XOR.}{}\label{logicalandor}
|
||||||
@ -831,8 +837,8 @@ the statements but not both are true. There is no operator for XOR in
|
|||||||
\end{table}
|
\end{table}
|
||||||
|
|
||||||
|
|
||||||
Table~\ref{logicalrelationaloperators} show the logical and relational
|
Tables~\ref{logicalrelationaloperators} show the logical and
|
||||||
operators that are available in \matlab{}. The additional
|
relational operators available in \matlab{}. The additional
|
||||||
\code[Operator!logical!and2@\&\&]{\&\&} and
|
\code[Operator!logical!and2@\&\&]{\&\&} and
|
||||||
\code[Operator!logical!or2@{"|}{"|} {}]{||} operators are the so
|
\code[Operator!logical!or2@{"|}{"|} {}]{||} operators are the so
|
||||||
called `\enterm{short-circuit} operators for the logical OR and
|
called `\enterm{short-circuit} operators for the logical OR and
|
||||||
@ -879,12 +885,12 @@ this saves processing time.
|
|||||||
\end{important}
|
\end{important}
|
||||||
|
|
||||||
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 in which it is
|
||||||
efficient to use a integer data type rather than storing floating
|
more efficient to use a integer data type rather than storing floating
|
||||||
point 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). \matlab{} knows a
|
||||||
a special data type (\codeterm{logical}) to store the result of a
|
special data type (\codeterm{logical}) to store the result of a
|
||||||
Boolean expression. Every variable can be evaluated to true or false
|
Boolean expression. Every variable can be evaluated to true or false
|
||||||
by converting 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
|
||||||
@ -922,20 +928,19 @@ ans = 1 0 1 1 0
|
|||||||
|
|
||||||
|
|
||||||
\section{Logical indexing}\label{logicalindexingsec}
|
\section{Logical indexing}\label{logicalindexingsec}
|
||||||
We have introduced how one can select certain element of a vector or
|
We have introduced how one can select elements of a vector or matrix
|
||||||
matrix by addressing the respective elements by their index. This is
|
by using their index. This is fine when we know the indices. There
|
||||||
fine when we know the range of elements we want t select. There are,
|
are, however, many situations in which a selection is based on the
|
||||||
however, many situations in which a selection based on the value of
|
value of the stored elements and the indices are not known. Such
|
||||||
the stored element is desired. These situations is one of the major
|
selections are one of the major places where we need Boolean
|
||||||
places where we need Boolean expressions. The selection based on the
|
expressions. The selection based on the result of a Boolean expression
|
||||||
result of a Boolean expression is called \enterm{logical
|
is called \enterm{logical indexing}. With this approach we can easily
|
||||||
indexing}. With this approach we can easily filter based on the
|
filter based on the values stored in a vector or matrix. It is very
|
||||||
values stored in a vector or matrix. It is very powerful and, once
|
powerful and, once understood, very intuitive.
|
||||||
understood, very intuitive.
|
|
||||||
|
|
||||||
The basic concept is that applying a Boolean operation on a vector
|
The basic concept is that applying a Boolean operation on a vector
|
||||||
results in a \code{logical} vector of the same size (see
|
results in a \code{logical} vector of the same size (see
|
||||||
listing~\ref{booleanexpressions}. This logical vector is then used to
|
listing~\ref{booleanexpressions}). This logical vector is then used to
|
||||||
select only those values for which the logical vector is true. Line 14
|
select only those values for which the logical vector is true. Line 14
|
||||||
in listing~\ref{logicalindexing1} can be read: ``Select all those
|
in listing~\ref{logicalindexing1} can be read: ``Select all those
|
||||||
elements of \varcode{x} where the Boolean expression \varcode{x < 0}
|
elements of \varcode{x} where the Boolean expression \varcode{x < 0}
|
||||||
@ -1007,7 +1012,7 @@ segment of data of a certain time span (the stimulus was on,
|
|||||||
|
|
||||||
\section{Control flow}\label{controlstructsec}
|
\section{Control flow}\label{controlstructsec}
|
||||||
|
|
||||||
Generally a program is executed line by line from top to
|
Generally, a program is executed line by line from top to
|
||||||
bottom. Sometimes this behavior is not wanted, or the other way round,
|
bottom. Sometimes this behavior is not wanted, or the other way round,
|
||||||
it is needed to skip certain parts or execute others
|
it is needed to skip certain parts or execute others
|
||||||
repeatedly. High-level programming languages like \matlab{} offer
|
repeatedly. High-level programming languages like \matlab{} offer
|
||||||
@ -1015,7 +1020,7 @@ statements that allow to manipulate the control flow. There are two
|
|||||||
major classes of such statements:
|
major classes of such statements:
|
||||||
|
|
||||||
\begin{enumerate}
|
\begin{enumerate}
|
||||||
\item loops.
|
\item loops
|
||||||
\item conditional expressions
|
\item conditional expressions
|
||||||
\end{enumerate}
|
\end{enumerate}
|
||||||
|
|
||||||
@ -1035,31 +1040,31 @@ x =
|
|||||||
120
|
120
|
||||||
\end{lstlisting}
|
\end{lstlisting}
|
||||||
|
|
||||||
Basically this kind of program is fine but it is rather
|
This kind of program is fine but it is rather repetitive. The only
|
||||||
repetitive. The only thing that changes is the increasing factor. The
|
thing that changes is the increasing factor. The repetition of such
|
||||||
repetition of such very similar lines of code is bad programming
|
very similar lines of code is bad programming style. This is not only
|
||||||
style. This is not only a matter of esthetics but there are severe
|
a matter of taste but there are severe drawbacks to this style:
|
||||||
drawbacks to this style:
|
|
||||||
\begin{enumerate}
|
\begin{enumerate}
|
||||||
\item Error-proneness: ``Copy-and-paste'' often leads to case that the
|
\item Error-proneness: ``Copy-and-paste'' often leads to the case that
|
||||||
essential part of a repetition is not adapted. \shortquote{Copy and
|
the essential part of a repetition is not adapted (the factor in the
|
||||||
paste is a design error.}{David Parnas}
|
example above). \shortquote{Copy and paste is a design error.}{David
|
||||||
|
Parnas}
|
||||||
\item Flexibility: The aforementioned program does exactly one thing,
|
\item Flexibility: The aforementioned program does exactly one thing,
|
||||||
it cannot be used for any other other purpose (such as the faculty
|
it cannot be used for any other other purpose (such as the faculty
|
||||||
of 6).
|
of 6) without a change.
|
||||||
\item Maintenance: If there is an error, it has to be fixed in all
|
\item Maintenance: If there is an error, it has to be fixed in all
|
||||||
repetitions. It is easy to forget a single change.
|
repetitions. It is easy to forget a single change.
|
||||||
\item Readability: repetitive code is terrible to read and to
|
\item Readability: repetitive code is terrible to read and to
|
||||||
understand. In parts one tends to skip repetitions (its the same,
|
understand. (I) one tends to skip repetitions (its the same,
|
||||||
anyways) and misses the essential change. Further, the duplication
|
anyways) and misses the essential change. (II), the duplication of
|
||||||
of code leads to long and hard to parse programs.
|
code leads to long and hard-to-parse programs.
|
||||||
\end{enumerate}
|
\end{enumerate}
|
||||||
All imperative programming languages offer a solution: the loop. It is
|
All imperative programming languages offer a solution: the loop. It is
|
||||||
used whenever the same commands have to be repeated.
|
used whenever the same commands have to be repeated.
|
||||||
|
|
||||||
|
|
||||||
\subsubsection{The \code{for} --- loop}
|
\subsubsection{The \code{for} --- loop}
|
||||||
The most common type of loop is the \codeterm{for-Schleife}. It
|
The most common type of loop is the \codeterm{for-loop}. It
|
||||||
consists of a \codeterm[Loop!head]{head} and the
|
consists of a \codeterm[Loop!head]{head} and the
|
||||||
\codeterm[Loop!body]{body}. The head defines how often the code of the
|
\codeterm[Loop!body]{body}. The head defines how often the code of the
|
||||||
body is executed. In \matlab{} the head begins with the keyword
|
body is executed. In \matlab{} the head begins with the keyword
|
||||||
@ -1148,16 +1153,16 @@ is only executed under a certain condition.
|
|||||||
\subsubsection{The \varcode{if} -- statement}
|
\subsubsection{The \varcode{if} -- statement}
|
||||||
|
|
||||||
The most prominent representative of the conditional expressions is
|
The most prominent representative of the conditional expressions is
|
||||||
the \code{it} statement (sometimes also called \code{if - else}
|
the \code{if} statement (sometimes also called \code{if - else}
|
||||||
statement). It constitutes a kind of branching point. It allows to
|
statement). It constitutes a kind of branching point. It allows to
|
||||||
control which code is executed.
|
control which branch of the code is executed.
|
||||||
|
|
||||||
Again, the statement consists of the head and the body. The head
|
Again, the statement consists of the head and the body. The head
|
||||||
begins with the keyword \code{if} followed by a Boolean expression
|
begins with the keyword \code{if} followed by a Boolean expression
|
||||||
that controls whether or not the body is entered. Optionally the body
|
that controls whether or not the body is entered. Optionally, the body
|
||||||
can be either ended by the \code{end} keyword or followed by
|
can be either ended by the \code{end} keyword or followed by
|
||||||
additional statements \code{elseif}, which allows to add another
|
additional statements \code{elseif}, which allows to add another
|
||||||
Boolean expression and to catch a certain condition or the \code{else}
|
Boolean expression and to catch another condition or the \code{else}
|
||||||
the provide a default case. The last body of the \code{if - elseif -
|
the provide a default case. The last body of the \code{if - elseif -
|
||||||
else} statement has to be finished with the \code{end}
|
else} statement has to be finished with the \code{end}
|
||||||
(listing~\ref{ifelselisting}).
|
(listing~\ref{ifelselisting}).
|
||||||
@ -1260,17 +1265,17 @@ end
|
|||||||
\end{lstlisting}
|
\end{lstlisting}
|
||||||
|
|
||||||
\begin{exercise}{logicalIndexingBenchmark.m}{logicalIndexingBenchmark.out}
|
\begin{exercise}{logicalIndexingBenchmark.m}{logicalIndexingBenchmark.out}
|
||||||
Above we claimed the logical indexing is faster and much more
|
Above we claimed that logical indexing is faster and much more
|
||||||
convenient than the manual selection of elements of a vector. By now
|
convenient than the manual selection of elements of a vector. By now
|
||||||
we have all the tools at hand to test this. \\
|
we have all the tools at hand to test this. \\
|
||||||
For this test create a large vector with 100000 (or more) random
|
For this test create a large vector with 100000 (or more) random
|
||||||
numbers. Filter from this vector all numbers that are less than 0.5
|
numbers. Filter from this vector all numbers that are less than 0.5
|
||||||
and copy them to a second vector. Surround you code with the brother
|
and copy them to a second vector. Surround you code with the brothers
|
||||||
\code{tic} and \code{toc} to have \matlab{} measure the time that
|
\code{tic} and \code{toc} to have \matlab{} measure the time that
|
||||||
has passed between the calls of \code{tic} and \code{toc}.
|
has passed between the calls of \code{tic} and \code{toc}.
|
||||||
|
|
||||||
\begin{enumerate}
|
\begin{enumerate}
|
||||||
\item Use a \code{for} loop to select the matching values.
|
\item Use a \code{for} loop to select matching values.
|
||||||
\item Use logical indexing.
|
\item Use logical indexing.
|
||||||
\end{enumerate}
|
\end{enumerate}
|
||||||
\end{exercise}
|
\end{exercise}
|
||||||
|
Reference in New Issue
Block a user