fixed page breaking of code and exercises
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
a = [2 4 6 8 10]; % row vector with five elements
|
||||
s = size(a) % store the return value of size() in a new variable
|
||||
s(2) % get the second element of s, i.e. the length along the 2nd dimension
|
||||
size(a,2) % the shortcut
|
||||
a = [2 4 6 8 10]; % row vector with five elements
|
||||
s = size(a) % store the return value of size() in a new variable
|
||||
s(2) % get the second element of s,
|
||||
% i.e. the length along the 2nd dimension
|
||||
size(a, 2) % the shortcut
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
s = 1 10
|
||||
ans = 10
|
||||
ans = 10
|
||||
s = 1 5
|
||||
ans = 5
|
||||
ans = 5
|
||||
|
||||
@@ -60,7 +60,8 @@ variable.
|
||||
In \matlab{} variables can be created at any time on the command line
|
||||
or any place in a script or function. Listing~\ref{varListing1} shows
|
||||
three different ways of creating a variable:
|
||||
\begin{lstlisting}[label=varListing1, caption={Creating variables.}]
|
||||
|
||||
\begin{pagelisting}[label=varListing1, caption={Creating variables.}]
|
||||
>> x = 38
|
||||
x =
|
||||
38
|
||||
@@ -72,7 +73,7 @@ y =
|
||||
>> z = 'A'
|
||||
z =
|
||||
A
|
||||
\end{lstlisting}
|
||||
\end{pagelisting}
|
||||
|
||||
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
|
||||
@@ -93,8 +94,7 @@ 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}).
|
||||
|
||||
\newpage
|
||||
\begin{lstlisting}[label=varListing2, caption={Requesting information about defined variables and their types.}]
|
||||
\begin{pagelisting}[label=varListing2, caption={Requesting information about defined variables and their types.}]
|
||||
>>class(x)
|
||||
ans =
|
||||
double
|
||||
@@ -110,7 +110,7 @@ x y z
|
||||
x 1x1 8 double
|
||||
y 0x0 0 double
|
||||
z 1x1 2 char
|
||||
\end{lstlisting}
|
||||
\end{pagelisting}
|
||||
|
||||
\begin{important}[Naming conventions]
|
||||
There are a few rules regarding variable names. \matlab{} is
|
||||
@@ -120,7 +120,6 @@ x y z
|
||||
variable names.
|
||||
\end{important}
|
||||
|
||||
\pagebreak[4]
|
||||
\subsection{Working with variables}
|
||||
We can certainly work, i.e. do calculations, with variables. \matlab{}
|
||||
knows all basic \entermde[Operator!arithmetic]{Operator!arithmetischer}{arithmetic operators}
|
||||
@@ -131,7 +130,7 @@ such as \code[Operator!arithmetic!1add@+]{+},
|
||||
\code[Operator!arithmetic!5pow@\^{}]{\^{}}. Listing~\ref{varListing3}
|
||||
shows their use.
|
||||
|
||||
\begin{lstlisting}[label=varListing3, caption={Working with variables.}]
|
||||
\begin{pagelisting}[label=varListing3, caption={Working with variables.}]
|
||||
>> x = 1;
|
||||
>> x + 10
|
||||
ans =
|
||||
@@ -149,13 +148,12 @@ ans =
|
||||
z =
|
||||
3
|
||||
|
||||
>> z = z * 5;
|
||||
>> z
|
||||
>> z = z * 5
|
||||
z =
|
||||
15
|
||||
|
||||
>> clear z % deleting a variable
|
||||
\end{lstlisting}
|
||||
\end{pagelisting}
|
||||
|
||||
Note: in lines 2 and 10 the variables have been used without changing
|
||||
their values. Whenever the value of a variable should change, the
|
||||
@@ -271,8 +269,8 @@ 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.}]
|
||||
|
||||
\begin{pagelisting}[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
|
||||
@@ -284,7 +282,7 @@ b =
|
||||
>> c = (0:2:10)
|
||||
c =
|
||||
0 2 4 6 8 10
|
||||
\end{lstlisting}
|
||||
\end{pagelisting}
|
||||
|
||||
The length of a vector, that is the number of elements, can be
|
||||
requested using the \code{length()} or \code{numel()}
|
||||
@@ -292,14 +290,14 @@ functions. \code{size()} provides the same information in a slightly,
|
||||
yet more powerful way (listing~\ref{vectorsizeslisting}). The above
|
||||
used vector \varcode{a} has the following size:
|
||||
|
||||
\begin{lstlisting}[label=vectorsizeslisting, caption={Size of a vector.}]
|
||||
\begin{pagelisting}[label=vectorsizeslisting, caption={Size of a vector.}]
|
||||
>> length(a)
|
||||
ans =
|
||||
10
|
||||
>> size(a)
|
||||
ans =
|
||||
1 10
|
||||
\end{lstlisting}
|
||||
\end{pagelisting}
|
||||
|
||||
The answer provided by the \code{size()} function demonstrates that
|
||||
vectors are nothing else but 2-dimensional matrices in which one
|
||||
@@ -310,7 +308,7 @@ create a column-vector and how the \code[Operator!Matrix!']{'} ---
|
||||
operator is used to transpose the column-vector into a row-vector
|
||||
(lines 14 and following).
|
||||
|
||||
\begin{lstlisting}[label=columnvectorlisting, caption={Column-vectors.}]
|
||||
\begin{pagelisting}[label=columnvectorlisting, caption={Column-vectors.}]
|
||||
>> b = [1; 2; 3; 4; 5; 6; 7; 8; 9; 10] % Creating a column-vector
|
||||
b =
|
||||
1
|
||||
@@ -333,7 +331,7 @@ b =
|
||||
>> size(b)
|
||||
ans =
|
||||
1 10
|
||||
\end{lstlisting}
|
||||
\end{pagelisting}
|
||||
|
||||
|
||||
\subsubsection{Accessing elements of a vector}
|
||||
@@ -354,7 +352,7 @@ number of elements irrespective of the type of vector.
|
||||
Elements of a vector are accessed via their index. This process is
|
||||
called \entermde{Indizierung}{indexing}.
|
||||
|
||||
In \matlab{} the first element has the index one.
|
||||
In \matlab{} the first element in a vector has the index one.
|
||||
|
||||
The last element's index equals the length of the vector.
|
||||
\end{important}
|
||||
@@ -365,25 +363,25 @@ individual values by providing a single index or use the
|
||||
\code[Operator!Matrix!:]{:} notation to access multiple values with a
|
||||
single command.
|
||||
|
||||
\begin{lstlisting}[label=vectorelementslisting, caption={Access to individual elements of a vector.}]
|
||||
>> a = (11:20)
|
||||
\begin{pagelisting}[label=vectorelementslisting, caption={Access to individual elements of a vector.}]
|
||||
>> a = (11:20) % generate a vector
|
||||
a =
|
||||
11 12 13 14 15 16 17 18 19 20
|
||||
|
||||
>> a(1) % the 1. element
|
||||
>> a(1) % the 1. element
|
||||
ans = 11
|
||||
>> a(5) % the 5. element
|
||||
>> a(5) % the 5. element
|
||||
ans = 15
|
||||
>> a(end) % the last element
|
||||
>> a(end) % the last element
|
||||
ans = 20
|
||||
\end{lstlisting}
|
||||
\end{pagelisting}
|
||||
|
||||
\begin{lstlisting}[caption={Access to multiple elements.}, label=vectorrangelisting]
|
||||
\begin{pagelisting}[caption={Access to multiple elements.}, label=vectorrangelisting]
|
||||
>> a([1 3 5]) % 1., 3. and 5. element
|
||||
ans =
|
||||
11 13 15
|
||||
|
||||
>> a(2:4) % all elements with the indices 2 to 4
|
||||
>> a(2:4) % elements at indices 2 to 4
|
||||
ans =
|
||||
12 13 14
|
||||
|
||||
@@ -394,7 +392,7 @@ ans =
|
||||
>> a(:) % all elements as row-vector
|
||||
ans =
|
||||
11 12 13 14 15 16 17 18 19 20
|
||||
\end{lstlisting}
|
||||
\end{pagelisting}
|
||||
|
||||
\begin{exercise}{vectorsize.m}{vectorsize.out}
|
||||
Create a row-vector \varcode{a} with 5 elements. The return value of
|
||||
@@ -403,13 +401,13 @@ ans =
|
||||
\end{exercise}
|
||||
|
||||
\begin{important}[The : (colon) operator]
|
||||
The colon \code[Operator!colon@:]{:} operator is often used when working with vectors. It has multiple purposes.
|
||||
The colon \code[Operator!colon@:]{:} operator is often used when working with vectors. It has multiple purposes.\vspace{-1ex}
|
||||
\begin{enumerate}
|
||||
\item In the simplest form, \code{x = a:b} with \code{a} and \code{b} being two numbers, it creates
|
||||
a vector \code{x} containing the numbers \code{a} to \code{b} in integer steps. In \matlab{} the borders $a$ and $b$ are included $[a, b]$ or $a\leq x \leq b$.
|
||||
\item In the form \code{x = a:c:b} the vector \code{x} uses a \emph{stepsize} of \code{c} to create the range of numbers.
|
||||
\item When used in the context of indexing such as \code{x(:)} all elements of the vector x are accessed.
|
||||
\item In the simplest form, \code{x = a:b} with \code{a} and \code{b} being two numbers, it generates
|
||||
a vector \code{x} containing the numbers \code{a} to \code{b} inclusively in integer steps.
|
||||
\item In the form \code{x = a:c:b} the vector \code{x} uses a \emph{stepsize} of \code{c} to generate the range of numbers.
|
||||
\item As vectors are often used for indexing in other vectors one use the colon operator to create such vectors implicitely, e.g. \varcode{x(1:2:end)} to access every seond element of \code{x}.
|
||||
\item Indexing with a single colon, e.g. \code{x(:)}, returns all elements of the vector \code{x} as a row vector.
|
||||
\end{enumerate}
|
||||
\end{important}
|
||||
|
||||
@@ -423,7 +421,7 @@ how vectors and scalars can be combined with the operators \code[Operator!arithm
|
||||
\code[Operator!arithmetic!4div@/]{/}
|
||||
\code[Operator!arithmetic!5powe@.\^{}]{.\^}.
|
||||
|
||||
\begin{lstlisting}[caption={Calculating with vectors and scalars.},label=vectorscalarlisting]
|
||||
\begin{pagelisting}[caption={Calculations with vectors and scalars.},label=vectorscalarlisting]
|
||||
>> a = (0:2:8)
|
||||
a =
|
||||
0 2 4 6 8
|
||||
@@ -447,7 +445,7 @@ ans =
|
||||
>> a .^ 2 % exponentiation
|
||||
ans =
|
||||
0 4 16 36 64
|
||||
\end{lstlisting}
|
||||
\end{pagelisting}
|
||||
|
||||
When doing calculations with scalars and vectors the same mathematical
|
||||
operation is done to each element of the vector. In case of, e.g. an
|
||||
@@ -460,7 +458,7 @@ element-wise operations of two vectors, e.g. each element of vector
|
||||
layout (row- or column vectors). Addition and subtraction are always
|
||||
element-wise (listing~\ref{vectoradditionlisting}).
|
||||
|
||||
\begin{lstlisting}[caption={Element-wise addition and subtraction of two vectors.},label=vectoradditionlisting]
|
||||
\begin{pagelisting}[caption={Element-wise addition and subtraction of two vectors.},label=vectoradditionlisting]
|
||||
>> a = [4 9 12];
|
||||
>> b = [4 3 2];
|
||||
>> a + b % addition
|
||||
@@ -479,7 +477,7 @@ Matrix dimensions must agree.
|
||||
>> a + d % both vectors must have the same layout!
|
||||
Error using +
|
||||
Matrix dimensions must agree.
|
||||
\end{lstlisting}
|
||||
\end{pagelisting}
|
||||
|
||||
Element-wise multiplication, division, or raising a vector to a given power requires a
|
||||
different operator with a preceding '.'. \matlab{} defines the
|
||||
@@ -489,7 +487,7 @@ following operators for element-wise operations on vectors
|
||||
\code[Operator!arithmetic!5powe@.\^{}]{.\^{}}
|
||||
(listing~\ref{vectorelemmultiplicationlisting}).
|
||||
|
||||
\begin{lstlisting}[caption={Element-wise multiplication, division and
|
||||
\begin{pagelisting}[caption={Element-wise multiplication, division and
|
||||
exponentiation of two vectors.},label=vectorelemmultiplicationlisting]
|
||||
>> a .* b % element-wise multiplication
|
||||
ans =
|
||||
@@ -509,7 +507,7 @@ Matrix dimensions must agree.
|
||||
>> a .* d % Both vectors must have the same layout!
|
||||
Error using .*
|
||||
Matrix dimensions must agree.
|
||||
\end{lstlisting}
|
||||
\end{pagelisting}
|
||||
|
||||
The simple operators \code[Operator!arithmetic!3mul@*]{*},
|
||||
\code[Operator!arithmetic!4div@/]{/} and
|
||||
@@ -519,7 +517,7 @@ matrix-operations known from linear algebra (Box~
|
||||
of a row-vectors $\vec a$ with a column-vector $\vec b$ the
|
||||
scalar-poduct (or dot-product) $\sum_i = a_i b_i$.
|
||||
|
||||
\begin{lstlisting}[caption={Multiplication of vectors.},label=vectormultiplicationlisting]
|
||||
\begin{pagelisting}[caption={Multiplication of vectors.},label=vectormultiplicationlisting]
|
||||
>> a * b % multiplication of two vectors
|
||||
Error using *
|
||||
Inner matrix dimensions must agree.
|
||||
@@ -536,14 +534,13 @@ ans =
|
||||
16 12 8
|
||||
36 27 18
|
||||
48 36 24
|
||||
\end{lstlisting}
|
||||
|
||||
\pagebreak[4]
|
||||
\end{pagelisting}
|
||||
|
||||
To remove elements from a vector an empty value
|
||||
(\code[Operator!Matrix!{[]}]{[]}) is assigned to the respective
|
||||
elements:
|
||||
\begin{lstlisting}[label=vectoreraselisting, caption={Deleting elements of a vector.}]
|
||||
|
||||
\begin{pagelisting}[label=vectoreraselisting, caption={Deleting elements of a vector.}]
|
||||
>> a = (0:2:8);
|
||||
>> length(a)
|
||||
ans = 5
|
||||
@@ -556,7 +553,7 @@ a = 4 8
|
||||
|
||||
>> length(a)
|
||||
ans = 2
|
||||
\end{lstlisting}
|
||||
\end{pagelisting}
|
||||
|
||||
In addition to deleting of vector elements one also add new elements
|
||||
or concatenate two vectors. When performing a concatenation the two
|
||||
@@ -718,7 +715,7 @@ remapping, but can be really helpful
|
||||
rows in each column and so on.}\label{matrixlinearindexingfig}
|
||||
\end{figure}
|
||||
|
||||
\begin{lstlisting}[label=matrixLinearIndexing, caption={Lineares indexing in matrices.}]
|
||||
\begin{pagelisting}[label=matrixLinearIndexing, caption={Lineares indexing in matrices.}]
|
||||
>> x = randi(100, [3, 4, 5]); % 3-D matrix filled with random numbers
|
||||
>> size(x)
|
||||
ans =
|
||||
@@ -735,17 +732,17 @@ ans =
|
||||
>> min(x(:)) % or even simpler
|
||||
ans =
|
||||
4
|
||||
\end{lstlisting}
|
||||
\end{pagelisting}
|
||||
|
||||
\matlab{} defines functions that convert subscript indices to linear indices and back (\code{sub2ind()} and \code{ind2sub()}).
|
||||
|
||||
\begin{ibox}[tp]{\label{matrixmultiplication} The matrix--multiplication.}
|
||||
\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 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
|
||||
\matlab{}.
|
||||
|
||||
The matrix--multiplication of two 2-D matrices is only possible if
|
||||
the number of columns in the first matrix agrees with the number of
|
||||
@@ -795,8 +792,7 @@ box~\ref{matrixmultiplication}). To do a matrix-multiplication the
|
||||
inner dimensions of the matrices must agree
|
||||
(box~\ref{matrixmultiplication}).
|
||||
|
||||
\pagebreak[4]
|
||||
\begin{lstlisting}[label=matrixOperations, caption={Two kinds of multiplications of matrices.}]
|
||||
\begin{pagelisting}[label=matrixOperations, caption={Two kinds of multiplications of matrices.}]
|
||||
>> A = randi(5, [2, 3]) % 2-D matrix
|
||||
A =
|
||||
1 5 3
|
||||
@@ -822,7 +818,7 @@ ans =
|
||||
10 15 20
|
||||
24 23 35
|
||||
16 17 25
|
||||
\end{lstlisting}
|
||||
\end{pagelisting}
|
||||
|
||||
\section{Boolean expressions}
|
||||
|
||||
@@ -854,7 +850,7 @@ synonymous for the logical values 1 and
|
||||
0. Listing~\ref{logicaldatatype} exemplifies the use of the logical
|
||||
data type.
|
||||
|
||||
\begin{lstlisting}[caption={The logical data type. Please note that the actual \matlab{} output looks a little different.}, label=logicaldatatype]
|
||||
\begin{pagelisting}[caption={The logical data type. Please note that the actual \matlab{} output looks a little different.}, label=logicaldatatype]
|
||||
>> true
|
||||
ans = 1
|
||||
>> false
|
||||
@@ -871,7 +867,7 @@ ans = 0
|
||||
ans = 1 1 1 1
|
||||
>> logical([1 2 3 4 0 0 10])
|
||||
ans = 1 1 1 1 0 0 1
|
||||
\end{lstlisting}
|
||||
\end{pagelisting}
|
||||
|
||||
\varcode{true} and \varcode{false} are reserved keywords that evaluate
|
||||
to the logical values 1 and 0, respectively. If you want to create a
|
||||
@@ -884,7 +880,7 @@ code of each character in ``test'' is non-zero value, thus, the result
|
||||
of casting it to logical is a vector of logicals. A similar thing
|
||||
happens upon casting a vector (or matrix) of numbers to logical. Each
|
||||
value is converted to logical and the result is true for all non-zero
|
||||
values (line 21).
|
||||
values (line 15).
|
||||
|
||||
Knowing how to represent true and false values in \matlab{} using the
|
||||
logical data type allows us to take a step towards more complex
|
||||
@@ -922,8 +918,8 @@ stored in variable \varcode{b}?''.
|
||||
|
||||
The result of such questions is then given as a logical
|
||||
value. Listing~\ref{relationaloperationslisting} shows examples using relational operators.
|
||||
\pagebreak
|
||||
\begin{lstlisting}[caption={Relational Boolean expressions.}, label=relationaloperationslisting]
|
||||
|
||||
\begin{pagelisting}[caption={Relational Boolean expressions.}, label=relationaloperationslisting]
|
||||
>> true == logical(1)
|
||||
ans = 1
|
||||
>> false ~= logical(1)
|
||||
@@ -941,7 +937,7 @@ ans = 0 0 1 0 0
|
||||
ans = 0 1 0 0 1
|
||||
>> [2 0 0 5 0] >= [1 0 3 2 0]
|
||||
ans = 1 1 0 1 1
|
||||
\end{lstlisting}
|
||||
\end{pagelisting}
|
||||
|
||||
Testing the relations between numbers and scalar variables is straight
|
||||
forward. When comparing vectors, the relational operator will be
|
||||
@@ -1038,7 +1034,7 @@ for implementing such
|
||||
expressions. Listing~\ref{logicaloperatorlisting} shows a few
|
||||
examples and respective illustrations are shown in figure~\ref{logicaloperationsfig}.
|
||||
|
||||
\begin{lstlisting}[caption={Boolean expressions.}, label=logicaloperatorlisting]
|
||||
\begin{pagelisting}[caption={Boolean expressions.}, label=logicaloperatorlisting]
|
||||
>> x = rand(1) % create a single random number in the range [0, 1]
|
||||
x = 0.3452
|
||||
>> x > 0.25 & x < 0.75
|
||||
@@ -1049,7 +1045,7 @@ x = 0.4920, 0.9106, 0.7218, 0.8749, 0.1574, 0.0201, 0.9107, 0.8357, 0.0357, 0.47
|
||||
ans = 1 0 1 0 0 0 0 0 0 1
|
||||
>> x < 0.25 | x > 0.75
|
||||
ans = 0 1 0 1 1 1 1 1 1 0
|
||||
\end{lstlisting}
|
||||
\end{pagelisting}
|
||||
|
||||
\begin{figure}[ht]
|
||||
\includegraphics[]{logical_operations}
|
||||
@@ -1064,7 +1060,6 @@ ans = 0 1 0 1 1 1 1 1 1 0
|
||||
data.}\label{logicaloperationsfig}
|
||||
\end{figure}
|
||||
|
||||
\pagebreak
|
||||
\begin{important}[Assignment and equality operators]
|
||||
The assignment operator \code[Operator!Assignment!=]{=} and the
|
||||
relational equality operator \code[Operator!relational!==]{==} are
|
||||
@@ -1093,7 +1088,7 @@ elements of \varcode{x} where the Boolean expression \varcode{x < 0}
|
||||
evaluates to true and store the result in the variable
|
||||
\varcode{x\_smaller\_zero}''.
|
||||
|
||||
\begin{lstlisting}[caption={Logical indexing.}, label=logicalindexing1]
|
||||
\begin{pagelisting}[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
|
||||
@@ -1110,7 +1105,7 @@ elements_smaller_zero =
|
||||
>> elements_smaller_zero = x(x < 0)
|
||||
elements_smaller_zero =
|
||||
-1.4023 -1.4224 -0.1774 -0.1961
|
||||
\end{lstlisting}
|
||||
\end{pagelisting}
|
||||
|
||||
\begin{exercise}{logicalVector.m}{logicalVector.out}
|
||||
Create a vector \varcode{x} containing the values 0--10.
|
||||
@@ -1119,8 +1114,7 @@ elements_smaller_zero =
|
||||
\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{enumerate}\vspace{-1ex}
|
||||
\end{exercise}
|
||||
|
||||
\begin{figure}[t]
|
||||
@@ -1142,7 +1136,6 @@ segment of data of a certain time span (the stimulus was on,
|
||||
\begin{exercise}{logicalIndexingTime.m}{}
|
||||
Assume that measurements have been made for a certain time. Usually
|
||||
measured values and the time are stored in two vectors.
|
||||
|
||||
\begin{itemize}
|
||||
\item Create a vector that represents the recording time \varcode{t
|
||||
= 0:0.001:10;}.
|
||||
@@ -1150,9 +1143,9 @@ segment of data of a certain time span (the stimulus was on,
|
||||
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 from 5--6\,s.
|
||||
\end{itemize}
|
||||
\item Use logical indexing to select values that have been
|
||||
recorded in the time span 5--6\,s.
|
||||
\end{itemize}\vspace{-1ex}
|
||||
\end{exercise}
|
||||
|
||||
\begin{ibox}[ht]{\label{advancedtypesbox}Advanced data types}
|
||||
@@ -1230,7 +1223,7 @@ As the name already suggests loops are used to execute the same parts
|
||||
of the code repeatedly. In one of the earlier exercises the factorial of
|
||||
five has been calculated as depicted in listing~\ref{facultylisting}.
|
||||
|
||||
\begin{lstlisting}[caption={Calculation of the factorial of 5 in five steps}, label=facultylisting]
|
||||
\begin{pagelisting}[caption={Calculation of the factorial of 5 in five steps}, label=facultylisting]
|
||||
>> x = 1;
|
||||
>> x = x * 2;
|
||||
>> x = x * 3;
|
||||
@@ -1239,7 +1232,7 @@ five has been calculated as depicted in listing~\ref{facultylisting}.
|
||||
>> x
|
||||
x =
|
||||
120
|
||||
\end{lstlisting}
|
||||
\end{pagelisting}
|
||||
|
||||
This kind of program solves the taks but it is rather repetitive. The only
|
||||
thing that changes is the increasing factor. The repetition of such
|
||||
@@ -1279,7 +1272,7 @@ a certain purpose. The \varcode{for}-loop is closed with the keyword
|
||||
\code{end}. Listing~\ref{looplisting} shows a simple version of such a
|
||||
for-loop.
|
||||
|
||||
\begin{lstlisting}[caption={Example of a \varcode{for}-loop.}, label=looplisting]
|
||||
\begin{pagelisting}[caption={Example of a \varcode{for}-loop.}, label=looplisting]
|
||||
>> for x = 1:3 % head
|
||||
disp(x) % body
|
||||
end
|
||||
@@ -1288,7 +1281,7 @@ for-loop.
|
||||
1
|
||||
2
|
||||
3
|
||||
\end{lstlisting}
|
||||
\end{pagelisting}
|
||||
|
||||
|
||||
\begin{exercise}{factorialLoop.m}{factorialLoop.out}
|
||||
@@ -1308,11 +1301,11 @@ keyword \code{while} 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 \varcode{while} loop.}, label=whileloop]
|
||||
while x == true % head with a Boolean expression
|
||||
\begin{pagelisting}[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}
|
||||
\end{pagelisting}
|
||||
|
||||
\begin{exercise}{factorialWhileLoop.m}{}
|
||||
Implement the factorial of a number \varcode{n} using a \varcode{while}-loop.
|
||||
@@ -1367,7 +1360,7 @@ expression to provide a default case. The last body of the
|
||||
\varcode{if} - \varcode{elseif} - \varcode{else} statement has to be
|
||||
finished with the \code{end} (listing~\ref{ifelselisting}).
|
||||
|
||||
\begin{lstlisting}[label=ifelselisting, caption={Structure of an \varcode{if} statement.}]
|
||||
\begin{pagelisting}[label=ifelselisting, caption={Structure of an \varcode{if} statement.}]
|
||||
if x < y % head
|
||||
% body I, executed only if x < y
|
||||
elseif x > y
|
||||
@@ -1375,7 +1368,7 @@ elseif x > y
|
||||
else
|
||||
% body III, executed only if the previous conditions did not match
|
||||
end
|
||||
\end{lstlisting}
|
||||
\end{pagelisting}
|
||||
|
||||
\begin{exercise}{ifelse.m}{}
|
||||
Draw a random number and check with an appropriate \varcode{if}
|
||||
@@ -1403,7 +1396,7 @@ that were not explicitly stated above (listing~\ref{switchlisting}).
|
||||
As usual the \code{switch} statement needs to be closed with an
|
||||
\code{end}.
|
||||
|
||||
\begin{lstlisting}[label=switchlisting, caption={Structure of a \varcode{switch} statement.}]
|
||||
\begin{pagelisting}[label=switchlisting, caption={Structure of a \varcode{switch} statement.}]
|
||||
mynumber = input('Enter a number:');
|
||||
switch mynumber
|
||||
case -1
|
||||
@@ -1413,7 +1406,7 @@ switch mynumber
|
||||
otherwise
|
||||
disp('something else');
|
||||
end
|
||||
\end{lstlisting}
|
||||
\end{pagelisting}
|
||||
|
||||
|
||||
\subsubsection{Comparison \varcode{if} and \varcode{switch} -- statements}
|
||||
@@ -1437,7 +1430,7 @@ skip the execution of the body under certain circumstances, one can
|
||||
use the keywords \code{break} and \code{continue}
|
||||
(listings~\ref{continuelisting} and \ref{continuelisting}).
|
||||
|
||||
\begin{lstlisting}[caption={Stop the execution of a loop using \varcode{break}.}, label=breaklisting]
|
||||
\begin{pagelisting}[caption={Stop the execution of a loop using \varcode{break}.}, label=breaklisting]
|
||||
>> x = 1;
|
||||
while true
|
||||
if (x > 3)
|
||||
@@ -1450,9 +1443,9 @@ use the keywords \code{break} and \code{continue}
|
||||
1
|
||||
2
|
||||
3
|
||||
\end{lstlisting}
|
||||
\end{pagelisting}
|
||||
|
||||
\begin{lstlisting}[caption={Skipping iterations using \varcode{continue}.}, label=continuelisting]
|
||||
\begin{pagelisting}[caption={Skipping iterations using \varcode{continue}.}, label=continuelisting]
|
||||
for x = 1:5
|
||||
if(x > 2 & x < 5)
|
||||
continue;
|
||||
@@ -1463,7 +1456,7 @@ end
|
||||
1
|
||||
2
|
||||
5
|
||||
\end{lstlisting}
|
||||
\end{pagelisting}
|
||||
|
||||
\begin{exercise}{logicalIndexingBenchmark.m}{logicalIndexingBenchmark.out}
|
||||
Above we claimed that logical indexing is faster and much more
|
||||
@@ -1535,11 +1528,11 @@ 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}).
|
||||
|
||||
\begin{lstlisting}[caption={Declaration of a function in \matlab{}}, label=functiondefinitionlisting]
|
||||
\begin{pagelisting}[caption={Declaration of a function in \matlab{}}, label=functiondefinitionlisting]
|
||||
function [y] = functionName(arg_1, arg_2)
|
||||
% ^ ^ ^
|
||||
% return value argument_1, argument_2
|
||||
\end{lstlisting}
|
||||
\end{pagelisting}
|
||||
|
||||
The keyword \code{function} is followed by the return value(s) (it can
|
||||
be a list \varcode{[]} of values), the function name and the
|
||||
@@ -1572,7 +1565,7 @@ The following listing (\ref{badsinewavelisting}) shows a function that
|
||||
calculates and displays a bunch of sine waves with different amplitudes.
|
||||
|
||||
|
||||
\begin{lstlisting}[caption={Bad example of a function that displays a series of sine waves.},label=badsinewavelisting]
|
||||
\begin{pagelisting}[caption={Bad example of a function that displays a series of sine waves.},label=badsinewavelisting]
|
||||
function myFirstFunction() % function head
|
||||
t = (0:0.01:2);
|
||||
frequency = 1.0;
|
||||
@@ -1583,7 +1576,7 @@ function myFirstFunction() % function head
|
||||
hold on;
|
||||
end
|
||||
end
|
||||
\end{lstlisting}
|
||||
\end{pagelisting}
|
||||
|
||||
\varcode{myFirstFunction} (listing~\ref{badsinewavelisting}) is a
|
||||
prime-example of a bad function. There are several issues with it's
|
||||
@@ -1644,7 +1637,7 @@ define (i) how to name the function, (ii) which information it needs
|
||||
Having defined this we can start coding
|
||||
(listing~\ref{sinefunctionlisting}).
|
||||
|
||||
\begin{lstlisting}[caption={Function that calculates a sine wave.}, label=sinefunctionlisting]
|
||||
\begin{pagelisting}[caption={Function that calculates a sine wave.}, label=sinefunctionlisting]
|
||||
function [time, sine] = sinewave(frequency, amplitude, t_max, t_step)
|
||||
% Calculate a sinewave of a given frequency, amplitude,
|
||||
% duration and temporal resolution.
|
||||
@@ -1662,7 +1655,7 @@ function [time, sine] = sinewave(frequency, amplitude, t_max, t_step)
|
||||
time = (0:t_step:t_max);
|
||||
sine = sin(frequency .* time .* 2 .* pi) .* amplitude;
|
||||
end
|
||||
\end{lstlisting}
|
||||
\end{pagelisting}
|
||||
|
||||
|
||||
\paragraph{II. Plotting a single sine wave}
|
||||
@@ -1689,7 +1682,7 @@ specification of the function:
|
||||
With this specification we can start to implement the function
|
||||
(listing~\ref{sineplotfunctionlisting}).
|
||||
|
||||
\begin{lstlisting}[caption={Function for the graphical display of data.}, label=sineplotfunctionlisting]
|
||||
\begin{pagelisting}[caption={Function for the graphical display of data.}, label=sineplotfunctionlisting]
|
||||
function plotFunction(x_data, y_data, name)
|
||||
% Plots x-data against y-data and sets the display name.
|
||||
%
|
||||
@@ -1701,7 +1694,7 @@ function plotFunction(x_data, y_data, name)
|
||||
% name : the displayname
|
||||
plot(x_data, y_data, 'displayname', name)
|
||||
end
|
||||
\end{lstlisting}
|
||||
\end{pagelisting}
|
||||
|
||||
|
||||
\paragraph{III. One script to rule them all}
|
||||
@@ -1728,7 +1721,7 @@ Again, we need to specify what needs to be done:
|
||||
|
||||
The implementation is shown in listing~\ref{sinesskriptlisting}.
|
||||
|
||||
\begin{lstlisting}[caption={Control script for the plotting of sine waves.},label=sinesskriptlisting]
|
||||
\begin{pagelisting}[caption={Control script for the plotting of sine waves.},label=sinesskriptlisting]
|
||||
amplitudes = 0.25:0.25:1.25;
|
||||
frequency = 2.0;
|
||||
duration = 10.0; % seconds
|
||||
@@ -1744,11 +1737,11 @@ for i = 1:length(amplitudes)
|
||||
end
|
||||
hold off
|
||||
legend('show')
|
||||
\end{lstlisting}
|
||||
\end{pagelisting}
|
||||
|
||||
\pagebreak[4]
|
||||
\begin{exercise}{plotMultipleSinewaves.m}{}
|
||||
Extend the program to plot also a range of frequencies.
|
||||
\pagebreak[4]
|
||||
\end{exercise}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user