some more language fixes

This commit is contained in:
Jan Grewe 2016-10-18 09:46:30 +02:00
parent 6204a45f23
commit 8be04a9b06

View File

@ -6,18 +6,17 @@
\subsection{Variables}
A \enterm{variable} is a pointer to a certain place in the computer's
memory. This pointer is characterized by its name, the variable's
name, and the \enterm{data type} (figure~\ref{variablefig}). In the
computer's memory the value of the variable is stored in binary form
that is as a sequence of zeros and ones (\enterm[Bit]{Bits}). When the
variable is read from the memory, this binary pattern is interpreted
according to the data type. The example shown in
figure~\ref{variablefig} shows that the very same bit pattern is either
interpreted as a 8-bit integer type (numeric value 38) or as a
ampersand (\&) character. In \matlab{} data types are of only minor
importance but there are occasions where it becomes important to know
the type of a variable and we will come back to them later on.
A \enterm{variable} is a pointer to a certain address in the
computer's memory. This pointer is characterized by it's name and the
\enterm{data type} (figure~\ref{variablefig}). In the computer's
memory the value of the variable is stored in binary form, that is, as
a sequence of zeros and ones (\enterm[Bit]{Bits}). When the variable
is read from the memory, this binary pattern is interpreted according
to the data type. The example shown in figure~\ref{variablefig} shows
that the very same bit pattern is either interpreted as an 8-bit
integer type (numeric value 38) or as the ampersand (\&) character. In
\matlab{} data types are of only minor importance but there are
occasions in which it becomes important to know the type of a variable.
\begin{figure}
\centering
@ -29,7 +28,7 @@ the type of a variable and we will come back to them later on.
\includegraphics[width=.8\textwidth]{variableB}
\label{variable:b}
\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
data type. The variable's value is stored as a pattern of binary
values (0 or 1). When reading the variable this pattern is
@ -56,8 +55,8 @@ z =
A
\end{lstlisting}
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
Line 1 can be read like: ``Create a variable with the name \varcode{x}
and assign the value 38''. The equality sign is the so called
\codeterm{assignment operator}. Line 5 defines a variable \varcode{y}
and assigns an empty value. If not explicitly specified \matlab{}
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
\codeterm{character}.
The actual data type of a variable can be found out with the
\code{class()} function. \code{who} prints a list of all defined
variables and \code{whos} provides detailed information
(listing~\ref{varListing2}).
There are two ways to find out the actual data type of a variable: the
\code{class()} and the \code{whos} functions. While \code{class()}
simply returns the data type, \code{whos} offers more detailed
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.}]
>>class(x)
@ -90,10 +91,11 @@ x y z
\end{lstlisting}
\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
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}
\subsection{Working with variables}
@ -104,7 +106,7 @@ such as \code[Operator!arithmetic!1add@+]{+},
\code[Operator!arithmetic!3mul@*]{*} and
\code[Operator!arithmetic!4div@/]{/}. The power is denoted by the
\code[Operator!arithmetic!5pow@\^{}]{\^{}}. Listing~\ref{varListing3}
show their use.
shows their use.
\pagebreak[4]
\begin{lstlisting}[label=varListing3, caption={Working with variables.}]
@ -133,10 +135,10 @@ z =
>> clear z % deleting a variable
\end{lstlisting}
Note: in lines 2 and 6 the values of the variables have been used
without changing their values. Whenever the value of a variable should
change, the \code[Operator!Assignment!=]{=} operator has to be used
(lines 14 and 18). Line 23, finally shows how to delete a variable.
Note: in lines 2 and 10 the variables have been used without changing
their values. Whenever the value of a variable should change, the
\code[Operator!Assignment!=]{=} operator has to be used (lines 14 and
18). Line 23, finally shows how to delete a variable.
\subsection{Data types}
@ -144,17 +146,19 @@ As mentioned above, the data type associated with a variable defines how the sto
\begin{itemize}
\item \codeterm{integer}: Integer numbers. There are several subtypes
which, for most use-cases, can be ignored when working in \matlab{}.
\item \codeterm{double}: Floating point numbers. In contrast to the
real numbers that are represented with this data type the number of
numeric values that can be represented is limited (countable?).
\item \codeterm{single} \codeterm{double}: Floating point numbers. In
contrast to the real numbers that are represented with this data
type the number of numeric values that can be represented is limited
(countable).
\item \codeterm{complex}: Complex numbers having a real and imaginary
part.
\item \codeterm{logical}: Boolean values that can be evaluated to
\code{true} or \code{false}.
\item \codeterm{char}: ASCII characters.
\end{itemize}
There is a variety of numeric data types that require different memory
demands and ranges of representable values (table~\ref{dtypestab}).
There is a variety of numeric data types that require different
amounts of memory and have different ranges of values that can be
represented (table~\ref{dtypestab}).
\begin{table}[t]
\centering
@ -162,8 +166,9 @@ demands and ranges of representable values (table~\ref{dtypestab}).
\label{dtypestab}
\begin{tabular}{llcl}\hline
Data type & memory demand & range & example \erh \\ \hline
\code{double} & 64 bit & $\approx -10^{308}$ to $\approx 10^{308}
$& Floating point numbers.\erb\\ \code{int} & 64 bit & $-2^{31}$
\code{single} & 32 bit & $\approx -3.4^{38}$ to $\approx 3.4^{38}$ & Floating point numbers.\erb \\
\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 &
$-2^{15}$ to $2^{15}-1$ & Digitizes measurements. \\ \code{uint8}
& 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.
\begin{ibox}[t]{\label{daqbox}Digitizing measurements}
Scenario: The electric activity (e.g. the membrane potential) of a
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
@ -217,9 +221,10 @@ Vectors and matrices are the most important data structures in
between theses structures, they are one- or multidimensional
\enterm{arrays}. Such arrays are structures that can store multiple
values of the same data type in a single variable. Due to \matlab{}'s
origin in the handling of mathematical problems, they have different
name but are internally the same. Vectors are 2-dimensional matrices
in which one dimension has the size 1 (a singleton dimension).
origin in the handling of mathematical problems, they are called
differently but are internally the same. Vectors are 2-dimensional
matrices in which one dimension has the size 1 (a singleton
dimension).
\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
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}
\titlecaption{Scalars and vectors.}{\textbf{A)} A scalar variable
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
1.''. Line 9 reads: ``Create a variable \varcode{c} and assign the
values from 0 to 10 in steps of 2''.
\pagebreak
\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 =
@ -309,18 +314,19 @@ ans =
\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
(figure~\ref{vectorindexingfig}). Each element has an individual
\codeterm{index} that ranges (int \matlab{}) from 1 to the number of
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]
Elements of a vector are accessed via their index. This process is
called \codeterm{indexing}.
@ -693,15 +699,15 @@ ans =
4
\end{lstlisting}
\begin{ibox}[t]{\label{matrixmultiplication} The matrix-multiplication.}
The matrix-multiplication from linear algebra is \textbf{not} an
element-wise multiplication of each element in a matrix \varcode{A}
and the respective element from matrix \varcode{B}. It is something
completely different. Confusing element-wise and
matrix-multiplication is one of the most common mistakes in
\begin{ibox}[tp]{\label{matrixmultiplication} The matrix--multiplication.}
The matrix--multiplication from linear algebra is \textbf{not} an
element--wise multiplication of each element in a matrix \varcode{A}
and the respective element of matrix \varcode{B}. It is something
completely different. Confusing element--wise and
matrix--multiplication is one of the most common mistakes in
\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
formal: $\mathbf{A}$ and $\mathbf{B}$ can be multiplied $(\mathbf{A}
\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
\varcode{true} or \varcode{false}. In the context of programming they
are used to test the relations accordingly the programming language
defines operators for such instructions. The following
\codeterm{relational operators} are defined:
(\code[Operator!relational!>]{>}, \code[Operator!relational!<]{<},
\code[Operator!relational!==]{==}, \code[Operator!relational!"~]{~},
greater than, less than, equal to, and not. Via so called
\codeterm[Operator!logical]{logical operators} it is possible to join
single Boolean expressions (\code[Operator!logical!and1@\&]{\&},
are used to test the relations between entities, i.e. are entities the
same, greater or less than? Accordingly, programming languages defines
operators for such instructions. The following \codeterm{relational
operators} are defined: (\code[Operator!relational!>]{>},
\code[Operator!relational!<]{<}, \code[Operator!relational!==]{==},
\code[Operator!relational!"~]{~}, greater than, less than, equal to,
and not. Using so called \codeterm[Operator!logical]{logical
operators} allows to join single Boolean expressions to more complex
constructs (\code[Operator!logical!and1@\&]{\&},
\code[Operator!logical!or1@{"|} {}]{|}, AND, OR). These expressions
are important to control which parts of the code should be evaluated
under a certain condition (conditional statements,
are important e.g. to control which parts of the code should be
evaluated under a certain condition (conditional statements,
Section~\ref{controlstructsec}) but also for accessing only certain
elements of a vector or matrix (logical indexing,
Section~\ref{logicalindexingsec}).
The truth tables (\ref{logicalandor}) are used to visualize the
results of Boolean expressions. The statements A and B can be
evaluated to True or False. When they are combined with a logical AND
the expression is true only if both statements are true. The logical
OR, 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
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,
B)}.
Truth tables (\ref{logicalandor}) are used to visualize the results of
Boolean expressions. A and B are statements that can be evaluated to
True or False. When they are combined with a logical AND the whole
expression is True only if both statements are True. The logical OR,
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
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, B)}.
\begin{table}[tp]
\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}
Table~\ref{logicalrelationaloperators} show the logical and relational
operators that are available in \matlab{}. The additional
Tables~\ref{logicalrelationaloperators} show the logical and
relational operators available in \matlab{}. The additional
\code[Operator!logical!and2@\&\&]{\&\&} and
\code[Operator!logical!or2@{"|}{"|} {}]{||} operators are the so
called `\enterm{short-circuit} operators for the logical OR and
@ -879,12 +885,12 @@ this saves processing time.
\end{important}
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
point numbers and discussed that there are instances in which it is
more efficient to use a integer data type rather than storing floating
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
store this information as a 0 (false) and 1 (true). \matlab{} knows a
special data type (\codeterm{logical}) to store the result of a
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
@ -922,20 +928,19 @@ ans = 1 0 1 1 0
\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.
We have introduced how one can select elements of a vector or matrix
by using their index. This is fine when we know the indices. There
are, however, many situations in which a selection is based on the
value of the stored elements and the indices are not known. Such
selections are 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
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{logicalindexing1} can be read: ``Select all those
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}
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,
it is needed to skip certain parts or execute others
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:
\begin{enumerate}
\item loops.
\item loops
\item conditional expressions
\end{enumerate}
@ -1035,31 +1040,31 @@ x =
120
\end{lstlisting}
Basically this kind of program is fine but it is rather
repetitive. The only thing that changes is the increasing factor. The
repetition of such very similar lines of code is bad programming
style. This is not only a matter of esthetics but there are severe
drawbacks to this style:
This kind of program is fine but it is rather repetitive. The only
thing that changes is the increasing factor. The repetition of such
very similar lines of code is bad programming style. This is not only
a matter of taste but there are severe drawbacks to this style:
\begin{enumerate}
\item Error-proneness: ``Copy-and-paste'' often leads to case that the
essential part of a repetition is not adapted. \shortquote{Copy and
paste is a design error.}{David Parnas}
\item Error-proneness: ``Copy-and-paste'' often leads to the case that
the essential part of a repetition is not adapted (the factor in the
example above). \shortquote{Copy and paste is a design error.}{David
Parnas}
\item Flexibility: The aforementioned program does exactly one thing,
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
repetitions. It is easy to forget a single change.
\item Readability: repetitive code is terrible to read and to
understand. In parts one tends to skip repetitions (its the same,
anyways) and misses the essential change. Further, the duplication
of code leads to long and hard to parse programs.
understand. (I) one tends to skip repetitions (its the same,
anyways) and misses the essential change. (II), the duplication of
code leads to long and hard-to-parse programs.
\end{enumerate}
All imperative programming languages offer a solution: the loop. It is
used whenever the same commands have to be repeated.
\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
\codeterm[Loop!body]{body}. The head defines how often the code of the
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}
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
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
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
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 -
else} statement has to be finished with the \code{end}
(listing~\ref{ifelselisting}).
@ -1260,17 +1265,17 @@ end
\end{lstlisting}
\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
we have all the tools at hand to test this. \\
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
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
has passed between the calls of \code{tic} and \code{toc}.
\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.
\end{enumerate}
\end{exercise}