fixed many index entries

This commit is contained in:
2019-12-09 20:01:27 +01:00
parent f24c14e6f5
commit bf52536b7b
12 changed files with 332 additions and 306 deletions

View File

@@ -112,7 +112,7 @@ x y z
\begin{important}[Naming conventions]
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. \varcode{x} and \varcode{X} are two different
names. Names must begin with an alphabetic character. German (or
other) umlauts, special characters and spaces are forbidden in
variable names.
@@ -689,9 +689,11 @@ then compare it to the elements on each page, and so on. An
alternative way is to make use of the so called \emph{linear indexing}
in which each element of the matrix is addressed by a single
number. The linear index thus ranges from 1 to
\code{numel(matrix)}. The linear index increases first along the 1st,
2nd, 3rd etc. dimension (figure~\ref{matrixlinearindexingfig}). It is
not as intuitive since one would need to know the shape of the matrix and perform a remapping, but can be really helpful
\code[numel()]{numel(matrix)}. The linear index increases first along
the 1st, 2nd, 3rd etc. dimension
(figure~\ref{matrixlinearindexingfig}). It is not as intuitive since
one would need to know the shape of the matrix and perform a
remapping, but can be really helpful
(listing~\ref{matrixLinearIndexing}).
@@ -882,10 +884,11 @@ table~\ref{logicaloperators}) which are introduced in the following
sections.
\subsection{Relational operators}
With \codeterm[Operator!relational]{relational operators} (table~\ref{relationaloperators})
we can ask questions such as: ''Is the value of variable \code{a}
larger than the value of \code{b}?'' or ``Is the value in \code{a}
equal to the one stored in variable \code{b}?''.
With \codeterm[Operator!relational]{relational operators}
(table~\ref{relationaloperators}) we can ask questions such as: ''Is
the value of variable \varcode{a} larger than the value of
\varcode{b}?'' or ``Is the value in \varcode{a} equal to the one
stored in variable \varcode{b}?''.
\begin{table}[h!]
\titlecaption{\label{relationaloperators}
@@ -930,13 +933,13 @@ Testing the relations between numbers and scalar variables is straight
forward. When comparing vectors, the relational operator will be
applied element-wise and compare the respective elements of the
left-hand-side and right-hand-side vectors. Note: vectors must have
the same length and orientation. The result of \code{[2 0 0 5 0] == [1
the same length and orientation. The result of \varcode{[2 0 0 5 0] == [1
0 3 2 0]'} in which the second vector is transposed to give a
column vector is a matrix!
\subsection{Logical operators}
With the relational operators we could for example test whether a
number is greater than a certain threshold (\code{x > 0.25}). But what
number is greater than a certain threshold (\varcode{x > 0.25}). But what
if we wanted to check whether the number falls into the range greater
than 0.25 but less than 0.75? Numbers that fall into this range must
satisfy the one and the other condition. With
@@ -1068,13 +1071,13 @@ 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
results in a \codeterm{logical} vector of the same size (see
listing~\ref{logicaldatatype}). 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}
evaluates to true and store the result in the variable
\emph{x\_smaller\_zero}''.
\varcode{x\_smaller\_zero}''.
\begin{lstlisting}[caption={Logical indexing.}, label=logicalindexing1]
>> x = randn(1, 6) % a vector with 6 random numbers
@@ -1154,13 +1157,14 @@ segment of data of a certain time span (the stimulus was on,
data and metadata in a single variable.
\textbf{Cell arrays} Arrays of variables that contain different
types. Unlike structures, the entries of a \codeterm{Cell array} are
not named. Indexing in \codeterm{Cell arrays} requires a special
operator the \code{\{\}}. \matlab{} uses \codeterm{Cell arrays} for
example when strings of different lengths should be stored in the
same variable: \varcode{months = \{'Januar', 'February', 'March',
'April', 'May', 'Jun'\};}. Note the curly braces that are used to
create the array and are also used for indexing.
types. Unlike structures, the entries of a \codeterm{cell array} are
not named. Indexing in \codeterm[cell array]{cell arrays} requires a
special operator the \code{\{\}}. \matlab{} uses \codeterm[cell
array]{cell arrays} for example when strings of different lengths
should be stored in the same variable: \varcode{months = \{'Januar',
'February', 'March', 'April', 'May', 'Jun'\};}. Note the curly
braces that are used to create the array and are also used for
indexing.
\textbf{Tables} Tabular structure that allows to have columns of
varying type combined with a header (much like a spreadsheet).
@@ -1170,8 +1174,8 @@ segment of data of a certain time span (the stimulus was on,
irregular intervals togehter with the measurement time in a single
variable. Without the \codeterm{Timetable} data type at least two
variables (one storing the time, the other the measurement) would be
required. \codeterm{Timetables} offer specific convenience functions
to work with timestamps.
required. \codeterm[Timetable]{Timetables} offer specific
convenience functions to work with timestamps.
\textbf{Maps} In a \codeterm{map} a \codeterm{value} is associated
with an arbitrary \codeterm{key}. The \codeterm{key} is not
@@ -1246,7 +1250,7 @@ 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}
\subsubsection{The \varcode{for} --- loop}
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 in the
@@ -1258,7 +1262,7 @@ next value of this vector. In the body of the loop any code can be
executed which may or may not use the running variable for a certain
purpose. The \code{for} loop is closed with the keyword
\code{end}. Listing~\ref{looplisting} shows a simple version of such a
\code{for} loop.
\codeterm{for-loop}.
\begin{lstlisting}[caption={Example of a \varcode{for}-loop.}, label=looplisting]
>> for x = 1:3 % head
@@ -1273,15 +1277,15 @@ purpose. The \code{for} loop is closed with the keyword
\begin{exercise}{factorialLoop.m}{factorialLoop.out}
Can we solve the factorial with a for-loop? Implement a for loop that
calculates the factorial of a number \varcode{n}.
Can we solve the factorial with a \varcode{for}-loop? Implement a
for loop that calculates the factorial of a number \varcode{n}.
\end{exercise}
\subsubsection{The \varcode{while} --- loop}
The \code{while}--loop is the second type of loop that is available in
almost all programming languages. Other, than the \code{for} -- loop,
The \codeterm{while-loop} is the second type of loop that is available in
almost all programming languages. Other, than the \codeterm{for-loop},
that iterates with the running variable over a vector, the while loop
uses a Boolean expression to determine when to execute the code in
it's body. The head of the loop starts with the keyword \code{while}
@@ -1289,22 +1293,22 @@ that is followed by a Boolean expression. If this can be evaluated to
true, the code in the body is executed. The loop is closed with an
\code{end}.
\begin{lstlisting}[caption={Basic structure of a \code{while} loop.}, label=whileloop]
\begin{lstlisting}[caption={Basic structure of a \varcode{while} loop.}, label=whileloop]
while x == true % head with a Boolean expression
% execute this code if the expression yields true
end
\end{lstlisting}
\begin{exercise}{factorialWhileLoop.m}{}
Implement the factorial of a number \varcode{n} using a \code{while}
-- loop.
Implement the factorial of a number \varcode{n} using a \varcode{while}-loop.
\end{exercise}
\begin{exercise}{neverendingWhile.m}{}
Implement a \code{while}--loop that is never-ending. Hint: the body
is executed as long as the Boolean expression in the head is
\code{true}. You can escape the loop by pressing \keycode{Ctrl+C}.
Implement a \varcode{while}-loop that is never-ending. Hint: the
body is executed as long as the Boolean expression in the head is
\varcode{true}. You can escape the loop by pressing
\keycode{Ctrl+C}.
\end{exercise}
@@ -1312,15 +1316,15 @@ end
\begin{itemize}
\item Both execute the code in the body iterative.
\item When using a \code{for} -- loop the body of the loop is executed
\item When using a \code{for}-loop the body of the loop is executed
at least once (except when the vector used in the head is empty).
\item In a \code{while} -- loop, the body is not necessarily
\item In a \code{while}-loop, the body is not necessarily
executed. It is entered only if the Boolean expression in the head
yields true.
\item The \code{for} -- loop is best suited for cases in which the
\item The \code{for}-loop is best suited for cases in which the
elements of a vector have to be used for a computation or when the
number of iterations is known.
\item The \code{while} -- loop is best suited for cases when it is not
\item The \code{while}-loop is best suited for cases when it is not
known in advance how often a certain piece of code has to be
executed.
\item Any problem that can be solved with one type can also be solve
@@ -1336,8 +1340,8 @@ is only executed under a certain condition.
\subsubsection{The \varcode{if} -- statement}
The most prominent representative of the conditional expressions is
the \code{if} statement (sometimes also called \code{if - else}
statement). It constitutes a kind of branching point. It allows to
the \codeterm{if statement} (sometimes also called \codeterm{if - else
statement}). It constitutes a kind of branching point. It allows to
control which branch of the code is executed.
Again, the statement consists of the head and the body. The head
@@ -1346,11 +1350,11 @@ 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 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 \varcode{if - elseif -
else} statement has to be finished with the \code{end}
(listing~\ref{ifelselisting}).
\begin{lstlisting}[label=ifelselisting, caption={Structure of an \code{if} statement.}]
\begin{lstlisting}[label=ifelselisting, caption={Structure of an \varcode{if} statement.}]
if x < y % head
% body I, executed only if x < y
elseif x > y
@@ -1361,7 +1365,7 @@ end
\end{lstlisting}
\begin{exercise}{ifelse.m}{}
Draw a random number and check with an appropriate \code{if}
Draw a random number and check with an appropriate \varcode{if}
statement whether it is
\begin{enumerate}
\item less than 0.5.
@@ -1373,9 +1377,9 @@ end
\subsubsection{The \varcode{switch} -- statement}
The \code{switch} statement is used whenever a set of conditions
The \codeterm{switch statement} is used whenever a set of conditions
requires separate treatment. The statement is initialized with the
\code{switch} keyword that is followed by \emph{switch expression} (a
\code{switch} keyword that is followed by a \emph{switch expression} (a
number or string). It is followed by a set of \emph{case expressions}
which start with the keyword \code{case} followed by the condition
that defines against which the \emph{switch expression} is tested. It
@@ -1412,7 +1416,7 @@ end
\end{itemize}
\subsection{The keywords \code{break} and \code{continue}}
\subsection{The keywords \varcode{break} and \varcode{continue}}
Whenever the execution of a loop should be ended or if you want to
skip the execution of the body under certain circumstances, one can
@@ -1458,7 +1462,7 @@ end
has passed between the calls of \code{tic} and \code{toc}.
\begin{enumerate}
\item Use a \code{for} loop to select matching values.
\item Use a \varcode{for} loop to select matching values.
\item Use logical indexing.
\end{enumerate}
\end{exercise}
@@ -1486,12 +1490,12 @@ and executed line-by-line from top to bottom.
\matlab{} knows three types of programs:
\begin{enumerate}
\item \codeterm[Script]{Scripts}
\item \codeterm[Function]{Functions}
\item \codeterm[Object]{Objects} (not covered here)
\item \entermde[script]{Skripte}{Scripts}
\item \entermde[function]{Funktion}{Functions}
\item \entermde[Object]{Objekte}{Objects} (not covered here)
\end{enumerate}
Programs are stored in so called \codeterm{m-files}
Programs are stored in so called \codeterm[m-file]{m-files}
(e.g. \file{myProgram.m}). To use them they have to be \emph{called}
from the command line or from within another program. Storing your code in
programs increases the re-usability. So far we have used
@@ -1507,13 +1511,13 @@ and if it now wants to read the previously stored variable, it will
contain a different value than expected. Bugs like this are hard to
find since each of the programs alone is perfectly fine and works as
intended. A solution for this problem are the
\codeterm[Function]{functions}.
\entermde[function]{Funktion}{functions}.
\subsection{Functions}
Functions in \matlab{} are similar to mathematical functions
\[ y = f(x) \] Here, the mathematical function has the name $f$ and it
has one \codeterm{argument} $x$ that is transformed into the
has one \entermde{Argument}{argument} $x$ that is transformed into the
function's output value $y$. In \matlab{} the syntax of a function
declaration is very similar (listing~\ref{functiondefinitionlisting}).
@@ -1524,12 +1528,12 @@ function [y] = functionName(arg_1, arg_2)
\end{lstlisting}
The keyword \code{function} is followed by the return value(s) (it can
be a list \code{[]} of values), the function name and the
be a list \varcode{[]} of values), the function name and the
argument(s). The function head is then followed by the function's
body. A function is ended by and \code{end} (this is in fact optional
but we will stick to this). Each function that should be directly used
by the user (or called from other programs) should reside in an
individual \code{m-file} that has the same name as the function. By
individual \codeterm{m-file} that has the same name as the function. By
using functions instead of scripts we gain several advantages:
\begin{itemize}
\item Encapsulation of program code that solves a certain task. It can
@@ -1566,10 +1570,9 @@ function myFirstFunction() % function head
end
\end{lstlisting}
\code{myFirstFunction} (listing~\ref{badsinewavelisting}) is a
\varcode{myFirstFunction} (listing~\ref{badsinewavelisting}) is a
prime-example of a bad function. There are several issues with it's
design:
\begin{itemize}
\item The function's name does not tell anything about it's purpose.
\item The function is made for exactly one use-case (frequency of
@@ -1594,7 +1597,7 @@ defined:
(e.g. the user of another program that calls a function)?
\end{enumerate}
As indicated above the \code{myFirstFunction} does three things at
As indicated above the \varcode{myFirstFunction} does three things at
once, it seems natural, that the task should be split up into three
parts. (i) Calculation of the individual sine waves defined by the
frequency and the amplitudes (ii) graphical display of the data and
@@ -1607,17 +1610,18 @@ define (i) how to name the function, (ii) which information it needs
(arguments), and (iii) what it should return to the caller.
\begin{enumerate}
\item \codeterm[Function!Name]{Name}: the name should be descriptive
\item \entermde[function!name]{Funktion!-sname}{Name}: the name should be descriptive
of the function's purpose, i.e. the calculation of a sine wave. A
appropriate name might be \code{sinewave()}.
\item \codeterm[Function!Arguments]{Arguments}: What information does
the function need to do the calculation? There are obviously the
frequency as well as the amplitude. Further we may want to be able
to define the duration of the sine wave and the temporal
resolution. We thus need four arguments which should also named to
describe their content: \code{amplitude, frequency, t\_max,} and
\code{t\_step} might be good names.
\item \codeterm[Function!Return values]{Return values}: For a correct
appropriate name might be \varcode{sinewave()}.
\item \entermde[function!arguments]{Funktion!-sargument}{Arguments}:
What information does the function need to do the calculation? There
are obviously the frequency as well as the amplitude. Further we may
want to be able to define the duration of the sine wave and the
temporal resolution. We thus need four arguments which should also
named to describe their content: \varcode{amplitude},
\varcode{frequency}, \varcode{t\_max}, and \varcode{t\_step} might
be good names.
\item \entermde[function!return values]{Funktion!R\"uckgabewerte}{Return values}: For a correct
display of the data we need two vectors. The time, and the sine wave
itself. We just need two return values: \varcode{time}, \varcode{sine}
\end{enumerate}
@@ -1657,11 +1661,11 @@ specification of the function:
\begin{enumerate}
\item It should plot a single sine wave. But it is not limited to sine
waves. It's name is thus: \code{plotFunction()}.
waves. It's name is thus: \varcode{plotFunction()}.
\item What information does it need to solve the task? The
to-be-plotted data as there is the values \code{y\_data} and the
corresponding \code{x\_data}. As we want to plot series of sine
waves we might want to have a \code{name} for each function to be
to-be-plotted data as there is the values \varcode{y\_data} and the
corresponding \varcode{x\_data}. As we want to plot series of sine
waves we might want to have a \varcode{name} for each function to be
displayed in the figure legend.
\item Are there any return values? No, this function is just made for
plotting, we do not need to return anything.
@@ -1699,11 +1703,11 @@ Again, we need to specify what needs to be done:
appropriate name for the script (that is the name of the m-file)
might be \file{plotMultipleSinewaves.m}.
\item What information do we need? we need to define the
\code{frequency}, the range of \code{amplitudes}, the
\code{duration} of the sine waves, and the temporal resolution given
as the time between to points in time, i.e. the \code{stepsize}.
\varcode{frequency}, the range of \varcode{amplitudes}, the
\varcode{duration} of the sine waves, and the temporal resolution given
as the time between to points in time, i.e. the \varcode{stepsize}.
\item We then need to create an empty figure, and work through the
rang of \code{amplitudes}. We must not forget to switch \code{hold
rang of \varcode{amplitudes}. We must not forget to switch \varcode{hold
on} if we want to see all the sine waves in one plot.
\end{enumerate}