diff --git a/programming/lecture/programming.tex b/programming/lecture/programming.tex index baba408..ea274c8 100644 --- a/programming/lecture/programming.tex +++ b/programming/lecture/programming.tex @@ -210,44 +210,46 @@ exemplifies such a case. \end{ibox} -\section{Vektoren und Matrizen} +\section{Vectors and matrices} -Vektoren und Matrizen sind die wichtigsten Datenstrukturen in -\matlab{}. In anderen Programmiersprachen hei{\ss}en sie ein- -bzw. mehrdimensionalen Felder. Felder sind Datenstrukturen, die -mehrere Werte des gleichen Datentyps in einer Variablen vereinen. Da -\matlab{} seinen Ursprung in der Verarbeitung von mathematischen -Vektoren und Matrizen hat, werden sie hier auch so genannt. Dabei -macht \matlab{} intern keinen Unterschied zwischen Vektoren und -Matrizen. Vektoren sind 2--dimensionale Matrizen, bei denen eine -Dimension die Gr\"o{\ss}e 1 hat. +Vectors and matrices are the most important data structures in +\matlab{}. In other programming languages there is no distinction +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). +\subsection{Vectors} -\subsection{Vektoren} - -Im Gegensatz zu Variablen, die einzelene Werte beinhalten -(Skalare), kann ein Vektor mehrere Werte des gleichen Datentyps -beinhalten (Abbildung \ref{vectorfig} B). Die Variable \varcode{a} -enth\"alt im Beispiel in Abbildung \ref{vectorfig} vier ganzzahlige Werte. +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} \includegraphics[width=0.8\columnwidth]{scalarArray} - \titlecaption{Skalare und Vektoren.}{\textbf{A)} Eine skalare Variable kann - genau einen Wert tragen. \textbf{B)} Ein Vektor kann mehrer - Werte des gleichen Datentyps (z.B. ganzzahlige Integer Werte) - beinhalten. \matlab{} kennt den Zeilen- (row-) und Spaltenvektoren - (columnvector).}\label{vectorfig} + \titlecaption{Scalars and vectors.}{\textbf{A)} A scalar variable + holds exactly on value. \textbf{B)} A vector can hold multiple + values. These must be of the same data type (e.g. integer + numbers). \matlab{} distinguishes between row- and + column-vectors.}\label{vectorfig} \end{figure} -Das folgende Listing \ref{generatevectorslisting} zeigt, wie Vektoren erstellt -werden k\"onnen. +The following listing (\ref{generatevectorslisting} shows how vectors +can be created. In lines 5 and 9 the \code[Operator!Matrix!:]{:} +notation is used to easily create vectors with many elements or with +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''. -\begin{lstlisting}[label=generatevectorslisting, caption={Erstellen einfacher Zeilenvektoren.}] ->> a = [0 1 2 3 4 5 6 7 8 9] % Erstellen eines Zeilenvektors +\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 ->> b = (0:9) % etwas bequemer +>> b = (0:9) % more comfortable b = 0 1 2 3 4 5 6 7 8 9 @@ -255,13 +257,14 @@ b = c = 0 2 4 6 8 10 \end{lstlisting} -Die L\"ange eines Vektors, d.h. die Anzahl der Elemente des Vektors, -kann mithilfe der Funktionen \code{length()} und \code{numel()} -bestimmt werden. \"Ahnliche Information kann \"uber die Funktion -\code{size()} erhalten werden (Listing \ref{vectorsizeslisting}). Der -Vektor \varcode{a} von oben hat folgende Gr\"o{\ss}en: -\begin{lstlisting}[label=vectorsizeslisting, caption={Gr\"o{\ss}e von Vektoren.}] +The length of a vector, that is the number of elements, can be +requested using the \code{length()} or \code{numel()} +functions. \code{size()} provides the same information in a slightly, +yet more powerful way (listing~\ref{vectorsizelisting}). The above +used vector \varcode{a} has the following size: + +\begin{lstlisting}[label=vectorsizeslisting, caption={Size of a vector.}] >> length(a) ans = 10 @@ -270,15 +273,17 @@ ans = 1 10 \end{lstlisting} -Die Ausgabe der \code{size()}-Funktion zeigt, dass Vektoren im Grunde -2-dimensional sind. Bei einem Zeilenvektor hat die erste Dimension die -Gr\"o{\ss}e 1. \code[length()]{length(a)} gibt die l\"angste -Ausdehnung an. Im folgenden Listing \ref{columnvectorlisting} transponiert der -\code[Operator!Matrix!']{'} - Operator einen Spaltenvektor -zu einem Zeilenvektor (Zeilen 14 ff.). - -\begin{lstlisting}[label=columnvectorlisting, caption={Spaltenvektoren.}] ->> b = [1; 2; 3; 4; 5; 6; 7; 8; 9; 10] % Erstellen eines Spaltenvektors +The answer provided by the \code{size()} function demonstrates that +vectors are nothing else but 2-dimensional matrices in which one +dimension has the size 1 (singleton dimension). +\code[length()]{length(a)} in line 1 just returns the size of the +largest dimension. Listing~\ref{columnvectorlisting} shows how to +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.}] +>> b = [1; 2; 3; 4; 5; 6; 7; 8; 9; 10] % Creating a column-vector b = 1 2 @@ -293,7 +298,7 @@ ans = ans = 10 1 ->> b = b' % Transponieren +>> b = b' % Transpose b = 1 2 3 4 5 6 7 8 9 10 @@ -303,75 +308,72 @@ ans = \end{lstlisting} -\subsubsection{Zugriff auf Inhalte von Vektoren} +\subsubsection{Accessing elements of a vector} \begin{figure} \includegraphics[width=0.4\columnwidth]{arrayIndexing} - \titlecaption{Indices von Vektoren.}{Jedes Feld eines Vektors hat - einen Index (kleine Zahl) mit dem auf den jeweiligen Inhalt - (gro{\ss}e Zahl) zugegriffen werden - kann.}\label{vectorindexingfig} + \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} -Der Zugriff auf die Inhalte eines Vektors erfolgt \"uber den Index -(Abbildung \ref{vectorindexingfig}). Jedes Feld in einem Vektor hat -einen fortlaufenden \codeterm{Index}, \"uber den auf die Werte des -Vektors zugegriffen werden kann. Dabei spielt es keine Rolle, ob es -sich um einen Zeilen- oder Spaltenvektor handelt. +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{important}[Indizieren] - Der Zugriff auf Inhalte eines Vektors mittels seines Indexes wird - Indizieren genannnt. +\begin{important}[Indexing] + Elements of a vector are accessed via their index. This process is + called \codeterm{indexing}. - Der Index des ersten Elements eines Vektors ist in \matlab{} die Eins. - - Der Index des letzten Elements entspricht der L\"ange des Vektors. + In \matlab{} the first element has the index one. + + The last element's index equals the length of the vector. \end{important} -Die Listings \ref{vectorelementslisting} und \ref{vectorrangelisting} zeigen wie -mit Indexen auf die Inhalte eines Vektors zugegriffen werden kann. -Hierbei kann auf einzelne Werte zugegriffen werden oder, analog zur -Erzeugung von Vektoren, die \code[Operator!Matrix!:]{:} Notation -verwendet werden, um auf mehrere Element gleichzeitig zuzugreifen. +Listings~\ref{vectorelementslisting} and~\ref{vectorrangelisting} show +how the index is used to access elements of a vector. One can access +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={Zugriff auf den Inhalt von Vektoren: einzelne Elemente}] +\begin{lstlisting}[label=vectorelementslisting, caption={Access to individual elements of a vector.}] >> a = (11:20) a = 11 12 13 14 15 16 17 18 19 20 ->> a(1) % das 1. Element +>> a(1) % the 1. element ans = 11 ->> a(5) % das 5. Element +>> a(5) % the 5. element ans = 15 ->> a(end) % das letzte Element +>> a(end) % the last element ans = 20 \end{lstlisting} -\begin{lstlisting}[caption={Zugriff auf den Inhalt von Vektoren: Bereiche}, label=vectorrangelisting] ->> a([1 3 5]) % das 1., 3. und 5. Element +\begin{lstlisting}[caption={Access to multiple elements.}, label=vectorrangelisting] +>> a([1 3 5]) % 1., 3. and 5. element ans = 11 13 15 ->> a(2:4) % alle Elemente von Index 2 bis einschliesslich 4 +>> a(2:4) % all elements with the indices 2 to 4 ans = 12 13 14 ->> a(1:2:end) % jedes zweite Element +>> a(1:2:end) % every second element ans = 11 13 15 17 19 ->> a(:) % alle Elemente als Zeilenvektor +>> a(:) % all elements as row-vector ans = 11 12 13 14 15 16 17 18 19 20 \end{lstlisting} \begin{exercise}{vectorsize.m}{vectorsize.out} - Erstelle einen Zeilenvektor \varcode{a} mit 5 Elementen. - Der R\"uckgabewert von \code[size()]{size(a)} ist wieder ein Vektor der - L\"ange 2. Wie k\"onnte also die Gr\"o{\ss}e von \varcode{a} in der - zweiten Dimension herausgefunden werden? + Create a row-vector \varcode{a} with 5 elements. The return value of + \code[size()]{size(a)} is a again a vector with the length 2. How + could you find out the size of the \varcode{a} in the 2nd dimension? \end{exercise} -\subsubsection{Operationen auf Vektoren} +\subsubsection{Operations with vectors} Mit Vektoren kann sehr einfach gerechnet werden. Listing \ref{vectorscalarlisting} zeigt die Verrechnung von Vektoren mit Skalaren