[exercises] separated matrix exercises
This commit is contained in:
parent
a509ba3ce3
commit
bd446b8c0c
244
programming/exercises/matrices.tex
Normal file
244
programming/exercises/matrices.tex
Normal file
@ -0,0 +1,244 @@
|
||||
\documentclass[12pt,a4paper,pdftex]{exam}
|
||||
|
||||
\usepackage[german]{babel}
|
||||
\usepackage{natbib}
|
||||
\usepackage{graphicx}
|
||||
\usepackage[small]{caption}
|
||||
\usepackage{sidecap}
|
||||
\usepackage{pslatex}
|
||||
\usepackage{amsmath}
|
||||
\usepackage{amssymb}
|
||||
\setlength{\marginparwidth}{2cm}
|
||||
\usepackage[breaklinks=true,bookmarks=true,bookmarksopen=true,pdfpagemode=UseNone,pdfstartview=FitH,colorlinks=true,citecolor=blue]{hyperref}
|
||||
|
||||
%%%%% text size %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
\usepackage[left=20mm,right=20mm,top=25mm,bottom=25mm]{geometry}
|
||||
\pagestyle{headandfoot}
|
||||
\header{{\bfseries\large Exercise 3}}{{\bfseries\large Matrices}}{{\bfseries\large 17. Oktober, 2017}}
|
||||
\firstpagefooter{Dr. Jan Grewe}{Phone: 29 74588}{Email:
|
||||
jan.grewe@uni-tuebingen.de}
|
||||
\runningfooter{}{\thepage}{}
|
||||
|
||||
\setlength{\baselineskip}{15pt}
|
||||
\setlength{\parindent}{0.0cm}
|
||||
\setlength{\parskip}{0.3cm}
|
||||
\renewcommand{\baselinestretch}{1.15}
|
||||
|
||||
\newcommand{\code}[1]{\texttt{#1}}
|
||||
\renewcommand{\solutiontitle}{\noindent\textbf{Solutions:}\par\noindent}
|
||||
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
\begin{document}
|
||||
\vspace*{-6.5ex}
|
||||
\begin{center}
|
||||
\textbf{\Large Introduction to Scientific Computing}\\[1ex]
|
||||
{\large Jan Grewe, Jan Benda}\\[-3ex]
|
||||
Neuroethology \hfill --- \hfill Institute for Neurobiology \hfill --- \hfill \includegraphics[width=0.28\textwidth]{UT_WBMW_Black_RGB} \\
|
||||
\end{center}
|
||||
|
||||
The exercises are meant for self-monitoring, revision of the lecture
|
||||
topic. You should try to solve them on your own. Your solution should
|
||||
be submitted as a single script (m-file) in the Ilias system. Each
|
||||
task should be solved in its own ``cell''. Each cell must be
|
||||
executable on its own. The file should be named according to the following pattern:
|
||||
``variables\_datatypes\_\{lastname\}.m'' benannt werden
|
||||
(e.g. variables\_datentypes\_mueller.m).
|
||||
|
||||
\begin{questions}
|
||||
|
||||
\question Erstelle folgende Matrix
|
||||
\[ A = \left( \begin{array}{ccc} 7 & 3 & 5 \\ 1 & 8 & 3 \\ 8 & 6 &
|
||||
4 \end{array} \right) \]
|
||||
\begin{parts}
|
||||
\part Benutze die Funktion \code{size} um die Gr\"o{\ss}e vpm \code{A} anzeeigen zu lassen.
|
||||
\begin{solution}
|
||||
\code{x = [7 3 5; 1 8 3; 8 6 4];\\disp(size(x))}
|
||||
\end{solution}
|
||||
\part Finde heraus, wie man \code{size} aufruft um nur die L\"ange entlang einer einzelnen Dimension auszugeben. Gib einzeln die L\"angen beider Dimensionen aus.
|
||||
\begin{solution}
|
||||
\code{disp(size(x, 1))}\\\code{disp(size(x, 2))}
|
||||
\end{solution}
|
||||
\part Gib das Element in der 3. Zeile und 2. Spalte aus.
|
||||
\begin{solution}
|
||||
\code{x(3,2)}
|
||||
\end{solution}
|
||||
\part Gib jeweils alle Elemente der 1., 2. und 3. Zeile aus.
|
||||
\begin{solution}
|
||||
\code{disp(x([1 2 3],:));}
|
||||
\end{solution}
|
||||
\part Gib jeweils alle Elemente der 1., 2., und 3. Spalte aus.
|
||||
\begin{solution}
|
||||
\code{disp(x(:, 1))\\ disp(x(:, 2))\\ disp(x(:, 3))}
|
||||
\end{solution}
|
||||
\part Erh\"ohe das Element in der 2. Zeile und 3. Spalte um Eins.
|
||||
\begin{solution}
|
||||
\code{x(2,3) = x(2,3) + 1;}
|
||||
\end{solution}
|
||||
\part Ziehe von allen Elementen der 1. Zeile 5 ab.
|
||||
\begin{solution}
|
||||
\code{x(1,:) = x(1,:) - 5;}
|
||||
\end{solution}
|
||||
\part Multipliziere alle Elementen der 3. Spalte mit 2.
|
||||
\begin{solution}
|
||||
\code{x(:,3) = x(:,3) * 2;}
|
||||
\end{solution}
|
||||
\end{parts}
|
||||
|
||||
\question Erstelle eine $5 \times 5$ Matrix \code{M} die
|
||||
Zufallszahlen enth\"alt (nutze die MATLAB Funktion
|
||||
\verb+randn()+. Benutze die Hilfe: Was macht die Funktion?).
|
||||
\begin{parts}
|
||||
\part Gib das Element in der 2. Zeile und 3. Spalte aus.
|
||||
\begin{solution}
|
||||
\code{M = randn(5, 5);}
|
||||
\code{disp(M(2,3))}
|
||||
\end{solution}
|
||||
|
||||
\part Gib jeweils alle Elemente der 1., 3. und letzten Zeile aus.
|
||||
\begin{solution}
|
||||
\code{disp(M(1,:)) \\ disp(M(3,:))\\ disp(M(size(M,1), :))}
|
||||
\end{solution}
|
||||
|
||||
\part Gib jeweils alle Elemente der 2. und 4. Spalte aus.
|
||||
\begin{solution}
|
||||
\code{disp(M(:,2))\\ disp(M(:,4))}
|
||||
\end{solution}
|
||||
|
||||
\part Greife mit einem einzigen Kommando auf die Elemente jeder
|
||||
zweiten Spalte zu und speichere die Daten in einer neuen Variable.
|
||||
\begin{solution}
|
||||
\code{y = M(:, [2:2:size(M,2)])}
|
||||
\end{solution}
|
||||
|
||||
\part Berechne jeweils den Mittelwert der 1., 3. und 5. Zeile
|
||||
(Funktion \code{mean}, siehe Hilfe).
|
||||
\begin{solution}
|
||||
\code{mean(M([1 3 5],:), 2)}
|
||||
\end{solution}
|
||||
|
||||
\part Berechne die Summe aller Werte der 2. und 4. Spalte
|
||||
(Funktion \code{sum}, siehe Hilfe).
|
||||
\begin{solution}
|
||||
\code{sum(M(:, [2 4]), 1)}
|
||||
\end{solution}
|
||||
|
||||
\part Berechne die Summe aller Elemente der Matrize.
|
||||
\begin{solution}
|
||||
\code{sum(M(:))}
|
||||
\end{solution}
|
||||
|
||||
\part Ersetze die Elemente der 2. Zeile mit denen der 4.
|
||||
\begin{solution}
|
||||
\code{M(2,:) = M(4,:)}
|
||||
\end{solution}
|
||||
|
||||
\part F\"uhre folgendes Kommando aus: \code{M(1:2,1) = [1, 2,
|
||||
3]}. Was k\"onnte die Absicht dieses Codes gewesen sein? Was
|
||||
bedeutet die Fehlermeldung?
|
||||
\begin{solution}
|
||||
\code{M(1:2,1) = [1, 2,3];\\ Subscripted assignment dimension
|
||||
mismatch.} \\ Der einzuf\"ugende Vektor hat 3 Elemente, die
|
||||
Auswahl von M in die geschrieben werden soll hat nur die
|
||||
Gr\"o{\ss}e 2;
|
||||
\end{solution}
|
||||
\end{parts}
|
||||
|
||||
\question Matrizen k\"onnen neben der ``normalen''
|
||||
\textit{subscript} Indizierung auch \textit{linear} indiziert werden
|
||||
(siehe Hilfe \"uber Indexing oder Funktionen \verb+sub2ind+ oder
|
||||
\verb+ind2sub+).
|
||||
\begin{parts}
|
||||
\part Erstelle eine 2-D Matrix mit Zufallszahlen mit der Dimensionalit\"at
|
||||
\verb+[10,10]+.
|
||||
\begin{solution}
|
||||
\code{x = randn(10, 10)}
|
||||
\end{solution}
|
||||
|
||||
\part Wie viele Werte enth\"alt sie?
|
||||
\begin{solution}
|
||||
\code{disp(numel(x))}
|
||||
\end{solution}
|
||||
|
||||
\part Benutze das lineare Indizieren um 50 zuf\"allige Werte
|
||||
auszuw\"ahlen.
|
||||
\begin{solution}
|
||||
\code{x(randi(100, 50, 1)])}
|
||||
\end{solution}
|
||||
|
||||
\part Wo liegt der Vorteil gegen\"uber der \textit{subscript}
|
||||
Indizierung?
|
||||
\begin{solution}
|
||||
Die Matrize ist 2-dimensional. Wenn mit dem subscript index
|
||||
zugegriffen werden soll, dann muss auf die Dimensionen
|
||||
einzeln geachtet werden. Mit dem linearen Indexieren kann einfach
|
||||
einen Vektor mit n Indices benutzt werden. Wenn es auch noch eine
|
||||
eindeutige (ohne doppelte) Auswahl sein soll, dann muss bei
|
||||
2-D viel komplexer kontrollieren.
|
||||
\end{solution}
|
||||
\part Berechne die Summe aller Werte mit einem Funktionsaufruf..
|
||||
\begin{solution}
|
||||
\code{sum(x(:))} oder \code{sum(sum(x))}
|
||||
\end{solution}
|
||||
|
||||
\end{parts}
|
||||
|
||||
\question 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 funktionieren
|
||||
sie nicht? Teste Deine Vorhersagen.
|
||||
\begin{parts}
|
||||
\part \code{x + y}
|
||||
\begin{solution}
|
||||
Funktioniert!
|
||||
\end{solution}
|
||||
|
||||
\part \code{x * M}
|
||||
\begin{solution}
|
||||
Matrixmultiplikation Funktioniert nicht! Inner dimensions must agree!
|
||||
\end{solution}
|
||||
|
||||
\part \code{x + y'}
|
||||
\begin{solution}
|
||||
Funktioniert nicht! Die Dimensionalit\"aten passen nicht.
|
||||
\end{solution}
|
||||
|
||||
\part \code{M - [x y]}
|
||||
\begin{solution}
|
||||
Funktioniert nicht! \code{[x y] ist ein Zeilenvektor der L\"ange
|
||||
6, M ist eine Martix.}
|
||||
\end{solution}
|
||||
|
||||
\part \code{[x; y]}
|
||||
\begin{solution}
|
||||
Funktioniert! Gr\"o{\ss}e: 2 3
|
||||
\end{solution}
|
||||
|
||||
\part \code{M - [x; y]}
|
||||
\begin{solution}
|
||||
Funktioniert!
|
||||
\end{solution}
|
||||
\end{parts}
|
||||
|
||||
\question 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{parts}
|
||||
\part Gib alle Elemente des ersten ``Blattes'' aus (Index 1 der 3. Dimension).
|
||||
\begin{solution}
|
||||
\code{x = randn(5,5); \\y = randn(5, 5);\\ z = cat(3, x, y);\\disp(z(:,:,1))}
|
||||
\end{solution}
|
||||
\end{parts}
|
||||
|
||||
\question Erzeuge eine $5 \times 5 \times 5$ Matrix die mit
|
||||
ganzzahligen, gleichverteilten Zufallszahlen zwischen 0 und 100
|
||||
gef\"ullt ist.
|
||||
\begin{parts}
|
||||
\part Berechne den Mittelwert aller Bl\"atter dieser Matrix
|
||||
(benutze \verb+mean()+, siehe Hilfe).
|
||||
\begin{solution}
|
||||
\code{x = round(rand(5,5,5) .* 100);\\ Disp(mean(mean(x(:,:,1))))\\ disp(mean(mean(x(:,:,2)))) \\ disp(mean(mean(x(:,:,3))))}
|
||||
\end{solution}
|
||||
\end{parts}
|
||||
\end{questions}
|
||||
|
||||
\end{document}
|
Reference in New Issue
Block a user