Matrices done

This commit is contained in:
Jan Grewe 2014-10-08 11:50:38 +02:00
parent a2d8eb2186
commit 4851365afc

View File

@ -102,12 +102,11 @@
\item Variablen und Datentypen
\item Vektoren und Matrizen
\item Boolsche Operationen
\item Was ist ein Programm
\item Kontrollstrukturen
\item Was ist ein Programm, Stil und Kommentare
\item Vom Problem zum Algorithmus
\item Skripte und Funktionen
\item Stil und Kommentare
\item Graphische Darstellung von Daten
\item Graphische Darstellung von Neuro Daten, PSTH, Rasterplot, STA
\item Fortgeschrittene Datenstrukturen
\item String Parsing
\item Lesen und schreiben von Dateien, navigieren im Dateisystem
@ -293,7 +292,7 @@
\begin{frame}[fragile]
\frametitle{Vektoren und Matrizen}
\framesubtitle{Erzeugen von Vektoren}
\footnotesize
\tiny
\begin{lstlisting}[label=arrayListing1]
>> a = [0 1 2 3 4 5 6 7 8 9] % Erstellen eines Zeilenvektors
a =
@ -349,7 +348,7 @@
\frametitle{Vektoren}
\framesubtitle{Zugriff auf Inhalte von Vektoren}
\vspace{-0.5cm}
\footnotesize
\tiny
\begin{lstlisting}
>> a = (11:20);
>> a(1) % das 1. Element
@ -372,7 +371,7 @@
\frametitle{Vektoren}
\framesubtitle{Zugriff auf Inhalte von Vektoren}
\vspace{-0.5cm}
\footnotesize
\tiny
\begin{lstlisting}
>> a([1 3 5]) % das 1., 3. und 5. Element
ans =
@ -394,7 +393,7 @@
\frametitle{Vektoren}
\framesubtitle{Grundlegende Operationen}
\vspace{-0.25cm}
\footnotesize
\tiny
\begin{lstlisting}[label=arrayListing4]
>> a = (0:2:8);
>> a + 5 % addiere einen Skalar
@ -413,13 +412,80 @@
ans =
2 6 10
>>
>> a(1:2) + a(2:4) % Addierte Vektoren muessen die gleiche Groesse haben!
>> a(1:2) + a(2:4) % Vektoren muessen gleich gro{\ss} sein!
??? Error using ==> plus
Matrix dimensions must agree.
\end{lstlisting}
\end{frame}
\begin{frame}[fragile]
\frametitle{Vektoren}
\framesubtitle{Grundlegende Operationen}
\vspace{-0.25cm}
\tiny
Wie bekomme ich Informationen \"uber einen Vektor?
\begin{lstlisting}
>> a = (0:2:8);
>> % die Laenge eines
>> length(a)
ans =
5
>>
>> size(a)
ans =
1 5
>>
\end{lstlisting}
\end{frame}
\begin{frame}[fragile]
\frametitle{Vektoren}
\framesubtitle{Grundlegende Operationen}
\vspace{-0.25cm}
L\"oschen von Elementen:
\tiny
\begin{lstlisting}
>> a = (0:2:8);
>> length(a)
ans =
5
>>
>> a(1) = [] % loesche das erste Element
a =
2 4 6 8
>> a([1 3]) = []
a =
4 8
>> length(a)
ans =
2
\end{lstlisting}
\end{frame}
\begin{frame}[fragile]
\frametitle{Vektoren}
\framesubtitle{Grundlegende Operationen}
\vspace{-0.25cm}
\tiny
Verkettung von Vektoren:
\begin{lstlisting}
>> a = (0:2:8);
>> b = (10:2:19);
>>
>> c = [a b] % erstelle einen Vektor aus einer Liste von Vektoren
c =
0 2 4 6 8 10 12 14 16 18
>> length(c)
ans =
10
>> length(a) + length(b)
ans =
10
\end{lstlisting}
\end{frame}
\begin{frame}[fragile]
\frametitle{Vektoren}
\framesubtitle{\"Ubungen}
@ -447,4 +513,263 @@
\end{enumerate}
\end{frame}
\begin{frame}{Matrizen}
\vspace{-0.5cm}
\begin{figure}
\centering
\includegraphics[width=0.65\columnwidth]{./images/matrices}
\end{figure}
\end{frame}
\begin{frame}[fragile]
\frametitle{Matrizen}
\framesubtitle{Erzeugen von Matrizen}
\tiny
\begin{lstlisting}
>> a = [1 2 3; 4 5 6; 7 8 9]
>> a =
1 2 3
4 5 6
7 8 9
>>
>>
>> b = ones(3,3,2);
>> b
b(:,:,1) =
1 1 1
1 1 1
1 1 1
b(:,:,2) =
1 1 1
1 1 1
1 1 1
\end{lstlisting}
\end{frame}
\begin{frame}
\frametitle{Matrizen}
\framesubtitle{Indexierung und Zugriff auf Inhalte}
\vspace{-0.5cm}
\begin{figure}
\centering
\includegraphics[width=0.9\columnwidth]{./images/matrixIndexing}
\end{figure}
\end{frame}
\begin{frame}[fragile]
\frametitle{Matrizen}
\framesubtitle{Indexierung}
\tiny
\begin{lstlisting}
>> x = roundi(100, [3, 4, 5]); % 3-D Matrix mit Zufallszahlen
>>
>> x(1,1,1); % obere linke Ecke
ans(1,1,1) =
14
>>
>> x(1,1,:) % obere linke Ecke entlang der 3. Dimension
ans(1,1,:) =
14
ans(:,:,2) =
58
ans(:,:,3) =
4
ans(:,:,4) =
93
ans(:,:,5) =
56
>>
\end{lstlisting}
\end{frame}
\begin{frame}[fragile]
\frametitle{Matrizen}
\framesubtitle{Grundlegende Operationen}
\vspace{-0.5 cm}
\tiny
\begin{lstlisting}
>> A = randi(10, [3, 3]) % 2-D Matrix
A =
3 8 2
2 10 3
10 7 1
>> B = randi(10, [3, 3]) % dto
B =
2 1 7
1 5 9
5 10 5
>>
>> A*B % Matrix Multiplikation
ans =
24 63 103
29 82 119
32 55 138
>>
>> A.*B % Elementweise Multiplikation
ans =
6 8 14
2 50 27
50 70 5
>>
\end{lstlisting}
\end{frame}
\begin{frame}[fragile]
\frametitle{Matrizen}
\framesubtitle{\"Ubungen}
\begin{enumerate}
\item Erstelle eine 5 x 5 Matrix die Zufallszahlen enth\"alt (nutze die
MATLAB Funktion \verb+randn()+, benutze die Hilfe. Was macht sie?).
\begin{enumerate}
\item Gib alle Elemente der ersten Zeile aus.
\item Gib alle Elemente der zweiten Spalte aus.
\item Greife mit einem einzigen Kommando auf die Elemnte jeder
2. Spalte zu und speichere die Daten in einer neuen Variable.
\end{enumerate}
\item Erstelle eine 3-D Matrix aus drei 2-D Matrizen. Benutze die
\verb+cat()+ Funktion f\"ur diesen Zweck ( schaue in der Hilfe
nach, wie sie benutzt wird).
\begin{enumerate}
\item Gib alle Elemente des ersten ``Blattes'' aus (Index 1 in der 3. Dimension).
\end{enumerate}
\item Erstelle eine 3-D Matrix mithilfe der Funktion
\verb+ones()+. Multipliziere das erste Blatt mit 1, das zweite mit
2, dritte mit 3 etc.
\end{enumerate}
\end{frame}
\begin{frame}[fragile]
\frametitle{Matrizen}
\framesubtitle{\"Ubungen}
\begin{enumerate}\setcounter{enumi}{3}
\item Erstelle folgende Variablen \verb+x = [1 5 9]+ and
\verb+y = [7 1 5]+ und \verb+M = [3 1 6; 5 2 7]+. Welche der
folgenden Operationen funktionieren? Wenn nicht, warum
funktioneieren sie nicht? Teste Deine Vorhersagen.
\begin{enumerate}
\item \begin{verbatim} x + y \end{verbatim}
\item \begin{verbatim} x * M \end{verbatim}
\item \begin{verbatim} x + y' \end{verbatim}
\item \begin{verbatim} M - [x y] \end{verbatim}
\item \begin{verbatim} [x; y] \end{verbatim}
\item \begin{verbatim} M - [x; y] \end{verbatim}
\end{enumerate}
\item Erzeuge eine 5 x 5 x 5 Matrix die mit ganzzahligen
Zufallszahlen zwischen 0 und 100 gefuellt ist.
\begin{enumerate}
\item Berechne den Mittelwert aller Bl\"atter dieser Matrix (benutze \verb+mean()+, siehe Hilfe).
\end{enumerate}
\end{enumerate}
\end{frame}
\begin{frame}[plain]
\huge{Boolesche Operationen}
\end{frame}
\begin{frame}
\frametitle{Boolesche Operationen}
\framesubtitle{Was ist das?}\pause
\end{frame}
\begin{frame}
\frametitle{Boolesche Operationen}
\framesubtitle{Logische Operatoren}
\begin{table}[th]
\begin{center}
\begin{tabular}{c|c}
\hline
\textbf{Operator} & \textbf{Beschreibung} \\ \hline
$\sim$ & logisches NOT\\
$\&$ & logisches UND\\
$|$ & logisches ODER\\
$\&\&$ & short-circuit logical AND\\
$\|$ & short-circuit logical OR\\
\hline
\end{tabular}
\end{center}
Das auschliessende ODER (XOR) ist nur als Funktion \verb+xor(A, B)+ verf\"ugbar.
\end{table}
\end{frame}
\subsection{Relational operators}
\begin{frame}
\frametitle{Boolesche Operationen}
\framesubtitle{Relationale Operatoren}
\begin{table}[th]
\begin{center}
\begin{tabular}{c|c}
\hline
\textbf{Operator} & \textbf{Beschreibung} \\ \hline
$<$ & kleiner als\\
$>$ & gr\"o\{ss}er als \\
$==$ & gleich \\
$>=$ & gr\"o\{ss}er oder gleich\\
$<=$ & kleiner oder gleich\\
$\sim=$ & ungleich\\
\hline
\end{tabular}
\end{center}
\end{table}
\end{frame}
\subsection{Logical operators}
\subsection{Boolean operations}
\begin{frame}[fragile]{Boolean operations}{Examples}
\tiny
\begin{lstlisting}[label=booleanListing1]
>> x = [2 0 0 5 0] & [1 0 3 2 0]
x =
1 0 0 1 0
>>
>> ~([2 0 0 5 0] & [1 0 3 2 0])
ans =
0 1 1 0 1
>> [2 0 0 5 0] | [1 0 3 2 0]
ans =
1 0 1 1 0
\end{lstlisting}
\end{frame}
\subsection{Exercises}
\begin{frame}[fragile]{Boolean operations}{Exercises}
\vspace{-0.5cm}
\begin{enumerate}
\item Given are two vectors \verb+x = [1 5 2 8 9 0 1]+ and
\verb+y = [5 2 2 6 0 0 2]+. Execute and explain the following commands.
\begin{enumerate}
\item \verb+x > y+
\item \verb+y < x+
\item \verb+x == y+
\item \verb+x ~= y+
\item \verb+x & ~y+
\item \verb+x | y+
\end{enumerate}
\item One can use boolean operations for so called logical indexing:
Given are \verb+x = 1:10+ and \verb+y = [3 1 5 6 8 2 9 4 7 0]+. Try
to understand the following commands.
\begin{enumerate}
\item \verb+x( (y <= 2) )+
\item \verb+x( (x > 2) | (y < 8) )+
\item \verb+x( (x == 0) & (y == 0) )+
\end{enumerate}
\item Play around with boolean operations.
\end{enumerate}
\end{frame}
\end{document}