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