[translate] matrices

This commit is contained in:
Jan Grewe 2016-10-11 18:21:58 +02:00
parent a565d94f00
commit e08b178717

View File

@ -375,156 +375,165 @@ ans =
\subsubsection{Operations with vectors} \subsubsection{Operations with vectors}
Mit Vektoren kann sehr einfach gerechnet werden. Listing Similarly to the scalar variables discussed above we can work with
\ref{vectorscalarlisting} zeigt die Verrechnung von Vektoren mit Skalaren vectors and do calculations. Listing~\ref{vectorscalarlisting} shows
mit den Operatoren \code[Operator!arithmetischer!1add@+]{+}, how vectors and scalars can be combined with the operators \code[Operator!arithmetic!1add@+]{+},
\code[Operator!arithmetischer!2sub@-]{-}, \code[Operator!arithmetic!2sub@-]{-},
\code[Operator!arithmetischer!3mul@*]{*}, \code[Operator!arithmetic!3mul@*]{*},
\code[Operator!arithmetischer!4div@/]{/} \code[Operator!arithmetic!4div@/]{/}
\code[Operator!arithmetischer!5powe@.\^{}]{.\^}. \code[Operator!arithmetic!5powe@.\^{}]{.\^}.
\begin{lstlisting}[caption={Rechnen mit Vektoren und Skalaren.},label=vectorscalarlisting] \begin{lstlisting}[caption={Cancluating with vectors and scalars.},label=vectorscalarlisting]
>> a = (0:2:8) >> a = (0:2:8)
a = a =
0 2 4 6 8 0 2 4 6 8
>> a + 5 % Addition von einem Skalar >> a + 5 % adding a scalar
ans = ans =
5 7 9 11 13 5 7 9 11 13
>> a - 5 % Subtraktion von einem Skalar >> a - 5 % subtracting a scalar
ans = ans =
-5 -3 -1 1 3 -5 -3 -1 1 3
>> a * 2 % Multiplikation mit einem Skalar >> a * 2 % multiplication
ans = ans =
0 4 8 12 16 0 4 8 12 16
>> a / 2 % Division mit einem Skalar >> a / 2 % division
ans = ans =
0 1 2 3 4 0 1 2 3 4
>> a .^ 2 % Potenzierung mit einem Skalar >> a .^ 2 % exponentiation
ans = ans =
0 4 16 36 64 0 4 16 36 64
\end{lstlisting} \end{lstlisting}
Bei der elementweisen Verrechnung von zwei Vektoren muss When calculating with scalars and vectors the same mathematical
sichergestellt werden, dass sie die gleiche L\"ange und das gleiche operation is done to each element of the vector. In case of, e.g. an
Layout (Spalten- oder Zeilenvektor) haben. Addition und Subtraktion addition this is called an element-wise addition.
erfolgt immer elementweise (Listing~\ref{vectoradditionlisting}).
Care has to be taken when you do calculations with two vectors. For
element-wise operations of two vectors, e.g. each element of vector
\varcode{a} should be added to the respective element of vector
\varcode{b} the two vectors must have the same length and the same
layout (row- or column vectors). Addition and subtraction are always
element-wise (listing~\ref{vectoradditionlisting}).
\begin{lstlisting}[caption={Elementweise Addition und Subtraktion von \begin{lstlisting}[caption={Element-wise addition and subtraction of two vectors.},label=vectoradditionlisting]
Vektoren.},label=vectoradditionlisting]
>> a = [4 9 12]; >> a = [4 9 12];
>> b = [4 3 2]; >> b = [4 3 2];
>> a + b % Addition von 2 Vektoren >> a + b % addition
ans = ans =
8 12 14 8 12 14
>> a - b % Subtraktion von 2 Vektoren >> a - b % subtraction
ans = ans =
0 6 10 0 6 10
>> c = [8 4]; >> c = [8 4];
>> a + c % Beide Vektoren muessen gleich gross sein! >> a + c % both vectors must have the same length!
Error using + Error using +
Matrix dimensions must agree. Matrix dimensions must agree.
>> d = [8; 4; 2]; >> d = [8; 4; 2];
>> a + d % Beide Vektoren muessen das gleiche Layout haben! >> a + d % both vectors must have the same layout!
Error using + Error using +
Matrix dimensions must agree. Matrix dimensions must agree.
\end{lstlisting} \end{lstlisting}
Bei der Multiplikation, der Division und der Potenzierung mu{\ss} mit Element-wise multiplication and division and exponentiation requires a
vorangestellem '.' angezeigt werden, dass es sich um eine different operator with preceding '.'. \matlab{} defines the
\emph{elementweise} Verarbeitung handeln soll. F\"ur diese following operators for element-wise operations on vectors
elementweisen Operationen kennt \matlab{} die Operatoren \code[Operator!arithmetic!3mule@.*]{.*},
\code[Operator!arithmetischer!3mule@.*]{.*}, \code[Operator!arithmetic!4dive@./]{./} and
\code[Operator!arithmetischer!4dive@./]{./} und \code[Operator!arithmetic!5powe@.\^{}]{.\^{}}
\code[Operator!arithmetischer!5powe@.\^{}]{.\^{}} (listing~\ref{vectorelemmultiplicationlisting}).
(Listing~\ref{vectorelemmultiplicationlisting}).
\begin{lstlisting}[caption={Element-wise multiplication, division and
\begin{lstlisting}[caption={Elementweise Multiplikation, Division und exponentiation of two vectors.},label=vectorelemmultiplicationlisting]
Potenzierung von Vektoren.},label=vectorelemmultiplicationlisting] >> a .* b % element-wise multiplication
>> a .* b % Elementweise Multiplikation
ans = ans =
16 27 24 16 27 24
>> a ./ b % Elementweise Division >> a ./ b % element-wise division
ans = ans =
1 3 6 1 3 6
>> a ./ b % Elementweise Potenzierung >> a ./ b % element-wise exponentiation
ans = ans =
256 729 144 256 729 144
>> a .* c % Beide Vektoren muessen gleich gross sein! >> a .* c % both vectors must have the same size!
Error using .* Error using .*
Matrix dimensions must agree. Matrix dimensions must agree.
>> a .* d % Beide Vektoren muessen das gleiche Layout haben! >> a .* d % Both vectors must have the same layout!
Error using .* Error using .*
Matrix dimensions must agree. Matrix dimensions must agree.
\end{lstlisting} \end{lstlisting}
Die einfachen Operatoren \code[Operator!arithmetischer!3mul@*]{*}, The simple operators \code[Operator!arithmetic!3mul@*]{*},
\code[Operator!arithmetischer!4div@/]{/} und \code[Operator!arithmetic!4div@/]{/} and
\code[Operator!arithmetischer!5pow@\^{}]{\^{}} sind mit den \code[Operator!arithmetic!5pow@\^{}]{\^{}} execute the respective
entsprechenden Matrixoperationen aus der linearen Algebrar belegt (Box matrix-operations known from linear algebra (Box~
\ref{matrixmultiplication}). Insbesondere ist die Multiplikation eines \ref{matrixmultiplication}). As a special case is the multiplication
Zeilenvektors $\vec a$ mit einem Spaltenvektor $\vec b$ das Skalarprodukt of a row-vectors $\vec a$ with a column-vector $\vec b$ the
$\sum_i = a_i b_i$. scalar-poduct (or dot-product) $\sum_i = a_i b_i$.
\begin{lstlisting}[caption={Multiplikation von Vektoren.},label=vectormultiplicationlisting] \begin{lstlisting}[caption={Multiplication of vectors.},label=vectormultiplicationlisting]
>> a * b % Multiplikation zweier Zeilenvektoren >> a * b % multiplication of two vectors
Error using * Error using *
Inner matrix dimensions must agree. Inner matrix dimensions must agree.
>> a' * b' % Multiplikation zweier Spaltenvektoren >> a' * b' % multiplication of column-vectors
Error using * Error using *
Inner matrix dimensions must agree. Inner matrix dimensions must agree.
>> a * b' % Multiplikation Zeilenvektor mit Spaltenvektor >> a * b' % multiplication of a row- and column-vector
ans = ans =
67 67
>> a' * b % Multiplikation Spaltenvektor mit Zeilenvektor >> a' * b % multiplication of a column- and a row-vector
ans = ans =
16 12 8 16 12 8
36 27 18 36 27 18
48 36 24 48 36 24
\end{lstlisting} \end{lstlisting}
\pagebreak[4] \pagebreak[4]
Zum Entfernen von Elementen aus einem Vektor, wird den
entsprechenden Zellen ein leeren Wert (\code[Operator!Matrix!{[]}]{[]}) zugewiesen: To remove elements from a vector an empty value
\begin{lstlisting}[label=vectoreraselisting, caption={L\"oschen von Elementen aus einem Vektor.}] (\code[Operator!Matrix!{[]}]{[]}) is assigned to the respective
elements:
\begin{lstlisting}[label=vectoreraselisting, caption={Deleting elements of a vector.}]
>> a = (0:2:8); >> a = (0:2:8);
>> length(a) >> length(a)
ans = 5 ans = 5
>> a(1) = [] % loesche das erste Element >> a(1) = [] % delete the 1st element
a = 2 4 6 8 a = 2 4 6 8
>> a([1 3]) = [] % loesche das erste und dritte Element >> a([1 3]) = [] % delete the 1st and 3rd element
a = 4 8 a = 4 8
>> length(a) >> length(a)
ans = 2 ans = 2
\end{lstlisting} \end{lstlisting}
Neben dem L\"oschen von Vektorinhalten k\"onnen Vektoren auch In addition to deleting of vector elements one also add new elements
erweitert oder zusammengesetzt werden. Auch hier muss das Layout der or concatenate two vectors. When performing a concatenation the two
Vektoren \"ubereinstimmen (Listing \ref{vectorinsertlisting}, Zeile concatenated vectors must match in their layout
10). Zum Erweitern eines Vektors kann \"uber das Ende hinaus (listing~\ref{vectorinsertlisting}, Line 11). To extend a vector we
zugewiesen werden (Zeile 20). \matlab{} erweitert dann die Variable can simply assign values beyond the end of the vector (line 21 in
entsprechend. Dieser Vorgang ist rechenintensiv da der ganze Vektor listing~ \ref{vectorinsertlisting}). \matlab{} will automatically
an eine neue Stelle im Arbeitsspeicher kopiert wird und sollte, soweit adjust the variable. This way of extending a vector on-the-fly is
m\"oglich, vermieden werden. however expensive. In the background \matlab{} has to reserve new
memory of the appropriate size and then copies the contents into
\begin{lstlisting}[caption={Zusammenf\"ugen und Erweitern von Vektoren.}, label=vectorinsertlisting] it. If possible this should be avoided (the \matlab{} editor will warn
you).
\begin{lstlisting}[caption={Concatenation and extension of vectors.}, label=vectorinsertlisting]
>> a = [4 3 2 1]; >> a = [4 3 2 1];
>> b = [10 12 14 16]; >> b = [10 12 14 16];
>> c = [a b] % erstelle einen Vektor aus einer Liste von Vektoren >> c = [a b] % create a new vector by concatenation
c = c =
4 3 2 1 10 12 14 16 4 3 2 1 10 12 14 16
>> length(c) >> length(c)
@ -532,45 +541,44 @@ ans = 8
>> length(a) + length(b) >> length(a) + length(b)
ans = 8 ans = 8
>> c = [a b']; % Vektorlayout muss uebereinstimmen >> c = [a b']; % vector layouts must match
Error using horzcat Error using horzcat
Dimensions of matrices being concatenated are not consistent. Dimensions of matrices being concatenated are not consistent.
>> a(1:3) = [5 6 7] % Weise den ersten drei Elementen neue Werte zu >> a(1:3) = [5 6 7] % assign new values to elements of the vector
a = a =
5 6 7 1 5 6 7 1
>> a(1:3) = [1 2 3 4]; % Laenge der Vektoren muss uebereinstimmen >> a(1:3) = [1 2 3 4]; % range of elements and number of new values must match
In an assignment A(I) = B, the number of elements in B and I must be the same. In an assignment A(I) = B, the number of elements in B and I must be the same.
>> a(3:6) = [1 2 3 4] % Zuweisung ueber die Laenge des Vektors hinweg >> a(3:6) = [1 2 3 4] % extending a vector by assigning beyond its bounds
a = a =
5 6 1 2 3 4 5 6 1 2 3 4
\end{lstlisting} \end{lstlisting}
\subsection{Matrizen} \subsection{Matrices}
Vektoren sind 1-dimensionale Spezialf\"alle von $n$-dimensionalen Vectors are a special case of the more general data structure,
Matrizen. Matrizen k\"onnen in \matlab{} beliebig viele Dimensionen i.e. the matrix. Vectors are matrices in which one dimension is a
haben. Von praktischer Bedeutung sind allerdings nur Matrizen mit bis singleton dimension (length of 1). While matrices can have an almost
zu vier Dimensionen. Meist beschr\"ankt es sich jedoch auf 2- bis 3-d arbitrary number of dimensions the most common matrices are 2-3
Matrizen (Abbildung \ref{matrixfig} A,B). dimensional (figure~\ref{matrixfig} A, B).
\begin{figure} \begin{figure}
\includegraphics[width=0.5\columnwidth]{matrices} \includegraphics[width=0.5\columnwidth]{matrices}
\titlecaption{Matrizen.}{\textbf{A)} Eine Variable (``test'') die eine \titlecaption{Matrices.}{\textbf{A)} 2-dimensional matrix with the
2-dimensionale Matrize ist. \textbf{B)} Illustration einer name ``test''. \textbf{B)} Illustration of a 3-dimensional
3-dimensionalen Matrize. Die Pfeile zeigen den Rang der matrix. Arrows indicate the rank across the dimensions.}\label{matrixfig}
Dimensionen an.}\label{matrixfig}
\end{figure} \end{figure}
Erzeugt werden Matrizen sehr \"ahnlich zu den Vektoren (Listing Matrices can be created similarly to vectors
\ref{matrixListing}). Die Definition einer Matrize wird, wie beim (listing~\ref{matrixlisting}). The definition of a matrix is enclosed
Vektor, durch \code[Operator!Matrix!{[]}]{[]} eingeschlossen. Das into the square braces \code[Operator!Matrix!{[]}]{[]} the semicolon
Semikolon \code[Operator!Matrix!;]{;} trennt die einzelnen Zeilen der operator \code[Operator!Matrix!;]{;} separates the individual rows of
Matrize. a matrix.
\begin{lstlisting}[label=matrixListing, caption={Erzeugen von Matrizen.}] \begin{lstlisting}[label=matrixlisting, caption={Creating matrices.}]
>> a = [1 2 3; 4 5 6; 7 8 9] >> a = [1 2 3; 4 5 6; 7 8 9]
>> a = >> a =
1 2 3 1 2 3
@ -588,39 +596,40 @@ b(:,:,2) =
1 1 1 1 1 1 1 1
\end{lstlisting} \end{lstlisting}
Zur Defintion von mehr-dimensionalen Matrizen ist die Notation in The notation shown in line 1 is not suited to create matrices of
Zeile 1 nicht geeignet. Es gibt allerdings eine Reihe von higher dimensions. For these, \matlab{} provides a number of
Helferfunktionen, die $n$-dimensionale Matrizen erstellen k\"onnen creator-functions that help creating n-dimensional matrices
(z.B. \code{ones()}, Zeile 7). Die \code{cat()}-Funktion kann (e.g. \code{ones()}, line 7 called with 3 arguments creates a 3-D
mehrdimensionale Matrizen zusammenzuf\"ugen. matrix). The function \code{cat()} allows to concatenate n-dimensional
matrices.
Um Informationen \"uber die Gr\"o{\ss}e einer Matrize zu bekommen ist To request the length of a vector we used the function
die Funktion \code{length()} nicht geeignet. Wie oben erw\"ahnt gibt sie \code{length()}. This function is no longer suited to request
die Gr\"o{\ss}e der l\"angsten Dimension aus. Die \code{size()}-Funktion information about the size of a matrix. As mentioned above,
gibt dagegen die L\"ange jeder Dimension als Vektor zur\"uck. \code{length()} would return the length of the largest dimension. The
function \code{size()} however, returns the length in each dimension
and should be always preferred over \code{length()}.
\begin{figure} \begin{figure}
\includegraphics[width=0.9\columnwidth]{matrixIndexing} \includegraphics[width=0.9\columnwidth]{matrixIndexing}
\titlecaption{Indices von Matrizen.}{Jedes Feld einer Matrize \titlecaption{Indices in matrices.}{Each element of a matrix is
wird durch einen Index individuell angesprochen. Der Index setzt identified by its index. The index is a tuple of as many numbers
sich aus so vielen Zahlen zusammen wie es Dimensionen gibt (links as the matrix has dimensions. The first coordinate in this tuple
2, rechts 3). Dabei steht die 1. Stelle immer f\"ur die Zeile, die counts the row, the second the column and the third the page,
2. f\"ur die Spalte und die dritte f\"ur das Blatt, etc. }\label{matrixindexingfig}
etc.. }\label{matrixindexingfig}
\end{figure} \end{figure}
Der Zugriff auf Inhalte von Matrizen erfolgt \"uber den Index Analogous to the element access in vectors we can address individual
(Abbildung \ref{matrixindexingfig}, Listing elements of a matrix by it's index. Similar to a coordinate system
\ref{matrixIndexing}). \"Ahnlich zu den Positionen in einem each element is addressed using a n-tuple whit n the number of
Koordinatensystem wird jede Zelle einer Matrize mit einem Index dimensions (figure~\ref{matrixindexingfig},
angesprochen, der aus $n$ Zahlen besteht wobei $n$ die listing~\ref{matrixIndexing}). This type of indexing is called
Dimensionalit\"at der Matrize ist. Diese Art des Zugriffs wird \codeterm{subscript indexing}. The first coordinate refers always to
\codeterm{subscript indexing} genannt. Dabei bestimmt die errste Zahl the row, the second to the column, the third to the page, and so on.
die Zeilennumer, die zweite die Splatennumer.
\begin{lstlisting}[caption={Zugriff auf Inhalte von Matrizen, \begin{lstlisting}[caption={Indexing in matrices,
Indizierung.}, label=matrixIndexing] Indizierung.}, label=matrixIndexing]
>> x=rand(3,4) % 2-D Matrix mit Zufallszahlen mit 3 Zeilen und 4 Spalten >> x=rand(3, 4) % 2-D matrix filled with random numbers
x = x =
0.8147 0.9134 0.2785 0.9649 0.8147 0.9134 0.2785 0.9649
0.9058 0.6324 0.5469 0.1576 0.9058 0.6324 0.5469 0.1576
@ -629,137 +638,146 @@ x =
ans = ans =
3 4 3 4
>> x(1,1) % obere linke Ecke >> x(1,1) % top left corner
ans = ans =
0.8147 0.8147
>> x(2,3) % Element der 2. Zeile, 3. Spalte >> x(2,3) % element in the 2nd row, 3rd column
ans = ans =
0.5469 0.5469
>> x(1,:) % erste Zeile >> x(1,:) % the first row
ans = ans =
0.8147 0.9134 0.2785 0.9649 0.8147 0.9134 0.2785 0.9649
>> x(:,2) % zweite Spalte >> x(:,2) % second column
ans = ans =
0.9134 0.9134
0.6324 0.6324
0.0975 0.0975
\end{lstlisting} \end{lstlisting}
Alternativ zum \codeterm{subscript indexing} k\"onnen die Zellen einer Subscript indexing is very intuitive but offers not always the most
Matrize auch \emph{linear} angesprochen werden (Abbildung straight-forward solution to the problem. Consider for example that
\ref{matrixlinearindexingfig}). Diese Art der Adressierung ist nicht you have a 3-D matrix and you want the minimal number in that
so intuitiv verst\"andlich, kann aber sehr hilfreich sein. Der lineare matrix. An alternative way is the so called \emph{linar indexing} in
Index einer Zelle reicht von 1 bis \code{numel()} Elemente. Wobei which each element of the matrix is addressed by a single number. The
dieser erst entlang der 1. Dimension, dann der 2., 3. etc. Dimension linear index thus ranges from 1 to \code{numel(matrix)}. The linear
ansteigt. Listing \ref{matrixLinearIndexing} zeigt ein Beispiel f\"ur index increases first along the 1st, 2nd, 3rd etc. dimension
den Einsatz des linearen Indizierens, z.B. zum Ermitteln des kleinsten (figure~\ref{matrixlinearindexingfig}). It is not as intuitive but can
Wertes in einer Matrize. be really helpful (listing~\ref{matrixLinearIndexing}).
\begin{figure} \begin{figure}
\includegraphics[width=0.9\columnwidth]{matrixLinearIndexing} \includegraphics[width=0.9\columnwidth]{matrixLinearIndexing}
\titlecaption{Lineares Indizieren von Matrizen.}{Der Index steigt \titlecaption{Linear indexing in matrices.}{The linear index in a
linear von 1 bis zur Anzahl Elemente in der Matrize an. Dabei matrix increases from 1 to the number of elements in the
steigt der Index zuerst entlang der ersten, zweiten, dritten und matrix. It increases first along the first dimension, then the
weiterer Dimensionen an.}\label{matrixlinearindexingfig} rows in each column and so on.}\label{matrixlinearindexingfig}
\end{figure} \end{figure}
\begin{lstlisting}[label=matrixLinearIndexing, caption={Lineares Indizieren in Matrizen.}] \begin{lstlisting}[label=matrixLinearIndexing, caption={Lineares indexing in matrices.}]
>> x = randi(100, [3, 4, 5]); % 3-D Matrix mit Zufallszahlen >> x = randi(100, [3, 4, 5]); % 3-D matrix filled with random numbers
>> size(x) >> size(x)
ans = ans =
3 4 5 3 4 5
>> numel(x) >> numel(x)
ans = ans =
60 60
>> min(min(min(x))) % Minimum ueber die Zeilen, Spalten, Blaetter... >> min(min(min(x))) % minimum across rows, then columns, then pages
ans = ans =
4 4
>> min(x(:)) % oder so >> min(x(1:numel(x))) % or like this
ans = ans =
4 4
>> min(x(:)) % or even simpler
ans =
4
\end{lstlisting} \end{lstlisting}
\begin{ibox}[t]{\label{matrixmultiplication} Matrixmultiplikation.} \begin{ibox}[t]{\label{matrixmultiplication} The matrix-multiplication.}
Die Matrixmuliplikation aus der linearen Algebra ist nicht eine The matrix-multiplication from linear algebra is \textbf{not} an
elementweise Multiplikation. Die Matrixmultiplikation ist nur dann element-wise multiplication of each element in a matrix \varcode{A}
m\"oglich, wenn die Anzahl Spalten der ersten Matrize gleich der and the respective element from matrix \varcode{B}. It is something
Anzahl Zeilen in der zweiten Matrize ist. Formaler: zwei Matrizen completely different. Confusing element-wise and
$\mathbf{A}$ und $\mathbf{B}$ k\"onnen mulipiziert $(\mathbf{A} matrix-multiplication is one of the most common mistakes in
\cdot \mathbf{B})$ werden, wenn $\mathbf{A}$ die Gr\"o{\ss}e $(m \times n)$ und \matlab{}. \linebreak
$\mathbf{B}$ die Gr\"o{\ss}e $(n \times k)$ hat. Die Mulitplikation ist
m\"oglich wenn die \determ{inneren Dimensionen} $n$ gleich sind. 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
Dann sind die Elemente $c_{i,j}$ des Matrixprodukts $\mathbf{C} = formal: $\mathbf{A}$ and $\mathbf{B}$ can be multiplied $(\mathbf{A}
\mathbf{A} \cdot \mathbf{B}$ gegeben durch das Skalarprodukt jeder \cdot \mathbf{B})$, if $\mathbf{A}$ has the size $(m \times n)$ and
Zeile von $\mathbf{A}$ mit jeder Spalte aus $\mathbf{B}$: $\mathbf{B}$ the size $(n \times k)$. The multiplication is possible
\[ c_{i,j} = \sum_{k=1}^n a_{i,k} \; b_{k,j} \; . \] if the \enterm{inner dimensions} $n$ agree.
Die Matrixmultiplikation ist im Allgemeinen auch nicht kommutativ: Then, the elements $c_{i,j}$ of the product $\mathbf{C} = \mathbf{A}
\cdot \mathbf{B}$ are given as the scalar product (dot-product) of
each row in $\mathbf{A}$ with each column in $\mathbf{B}$: \[
c_{i,j} = \sum_{k=1}^n a_{i,k} \; b_{k,j} \; . \]
The matrix-multiplication is not commutative, that is:
\[ \mathbf{A} \cdot \mathbf{B} \ne \mathbf{B} \cdot \mathbf{A} \; . \] \[ \mathbf{A} \cdot \mathbf{B} \ne \mathbf{B} \cdot \mathbf{A} \; . \]
Als Beispiel betrachten wir die beiden Matrizen Consider the matrices:
\[\mathbf{A}_{(3 \times 2)} = \begin{pmatrix} 1 & 2 \\ 5 & 4 \\ -2 & 3 \end{pmatrix} \[\mathbf{A}_{(3 \times 2)} = \begin{pmatrix} 1 & 2 \\ 5 & 4 \\ -2 & 3 \end{pmatrix}
\quad \text{und} \quad \mathbf{B}_{(2 \times 2)} = \begin{pmatrix} \quad \text{and} \quad \mathbf{B}_{(2 \times 2)} = \begin{pmatrix}
-1 & 2 \\ -2 & 5 \end{pmatrix} \; . \] -1 & 2 \\ -2 & 5 \end{pmatrix} \; . \] The inner dimensions of
F\"ur das Produkt $\mathbf{A} \cdot \mathbf{B}$ stimmen die inneren these matrices match ($(3 \times 2) \cdot (2 \times 2)$) and the
Dimensionen der Matrizen \"uberein ($(3 \times 2) \cdot (2 product of $\mathbf{C} = \mathbf{A} \cdot \mathbf{B}$ can be
\times 2)$), die Matrixmultiplikation ist also m\"oglich. Nachdem calculated. Following from the number of rows in $\mathbf{A}$ (3)
$\mathbf{A}$ drei Zeilen und $\mathbf{B}$ zwei Spalten hat, hat das and the number of columns in $\mathbf{B}$ (2) the resulting matrix
Ergebnis von $\mathbf{A} \cdot \mathbf{B}$ die Gr\"o{\ss}e $(3 $\mathbf{C}$ will have the size $(3 \times 2)$:
\times 2)$:
\[ \mathbf{A} \cdot \mathbf{B} = \begin{pmatrix} 1 \cdot -1 + 2 \cdot -2 & 1 \cdot 2 + 2\cdot 5 \\ \[ \mathbf{A} \cdot \mathbf{B} = \begin{pmatrix} 1 \cdot -1 + 2 \cdot -2 & 1 \cdot 2 + 2\cdot 5 \\
5 \cdot -1 + 4 \cdot -2 & 5 \cdot 2 + 4 \cdot 5\\ 5 \cdot -1 + 4 \cdot -2 & 5 \cdot 2 + 4 \cdot 5\\
-2 \cdot -1 + 3 \cdot -2 & -2 \cdot 2 + 3 \cdot 5 \end{pmatrix} -2 \cdot -1 + 3 \cdot -2 & -2 \cdot 2 + 3 \cdot 5 \end{pmatrix}
= \begin{pmatrix} -5 & 12 \\ -13 & 30 \\ -4 & 11\end{pmatrix} \; . \] = \begin{pmatrix} -5 & 12 \\ -13 & 30 \\ -4 & 11\end{pmatrix} \; . \]
Das Produkt $\mathbf{B} \cdot \mathbf{A}$ ist dagegen nicht The product of $\mathbf{B} \cdot \mathbf{A}$, however, is not
definiert, da die inneren Dimensionen nicht \"ubereinstimmen defined since the inner dimensions do not agree ($(2 \times 2) \cdot
($(2 \times 2) \cdot (3 \times 2)$). (3 \times 2)$).
\end{ibox} \end{ibox}
Beim Rechnen mit Matrizen gelten die gleichen Regeln wie bei Calculations on matrices apply the same rules as the calculations with
Vektoren. Matrizen k\"onnen solange elementweise miteinander vectors. Element-wise computations are possible as long as the
verrechnet werden, wie die Dimensionalit\"aten matrices have the same dimensionality. It is again important to
\"ubereinstimmen. Wichtig ist auch hier wieder die Unterscheidung distinguish between the element-wise
zwischen elementweiser Multiplikation (\code[Operator!arithmetic!3mule@.*]{.*} operator, listing
(\code[Operator!arithmetischer!3mule@.*]{.*} Operator, Listing \ref{matrixOperations} line 10) and the operator for
\ref{matrixOperations} Zeile 10) oder Matrixmultiplikation matrix-multiplication (\code[Operator!arithmetic!3mul@*]{*},
(\code[Operator!arithmetischer!3mul@*]{*} Operator, Listing listing~\ref{matrixOperations} lines 14, 17 and 21,
\ref{matrixOperations} Zeile 14, 17 und 21, Box~\ref{matrixmultiplication}). box~\ref{matrixmultiplication}). To do a matrix-multiplication the
Bei der Matrixmultiplikation m\"ussen die inneren Dimensionen der Matrizen \"ubereinstimmen inner dimensions of the matrices have to agree
(Box~\ref{matrixmultiplication}). (box~\ref{matrixmultiplication}).
\pagebreak[4] \pagebreak[4]
\begin{lstlisting}[label=matrixOperations, caption={Zwei Arten der Multiplikation von Matrizen.}] \begin{lstlisting}[label=matrixOperations, caption={Two kinds of multiplications of matrices.}]
>> A = randi(5, [2, 3]) % 2-D Matrix >> A = randi(5, [2, 3]) % 2-D matrix
A = A =
1 5 3 1 5 3
3 2 2 3 2 2
>> B = randi(5, [2, 3]) % dito >> B = randi(5, [2, 3]) % dto.
B = B =
4 3 5 4 3 5
2 4 5 2 4 5
>> A .* B % elementweise Multiplikation >> A .* B % element-wise multiplication
ans = ans =
4 15 15 4 15 15
6 8 10 6 8 10
>> A * B % Matrixmultiplikation >> A * B % invalid matrix-multiplication
Error using * Error using *
Inner matrix dimensions must agree. Inner matrix dimensions must agree.
>> A * B' % Matrixmultiplikation >> A * B' % valid matrix-multiplication
ans = ans =
34 37 34 37
28 24 28 24
>> A' * B % Matrixmultiplikation >> A' * B % matrix-multiplication is not commutative
ans = ans =
10 15 20 10 15 20
24 23 35 24 23 35
16 17 25 16 17 25
\end{lstlisting} \end{lstlisting}
\section{Boolesche Operationen} \section{Boolean Operations}
Boolesche Ausdr\"ucke sind Anweisungen, die zu \codeterm{wahr} oder Boolesche Ausdr\"ucke sind Anweisungen, die zu \codeterm{wahr} oder
\codeterm{falsch} ausgewertet werden. Man kennt sie z.B. aus der \codeterm{falsch} ausgewertet werden. Man kennt sie z.B. aus der