From a3d6e590c2d2d7d6ab53323ef995f0a0b0ea4430 Mon Sep 17 00:00:00 2001 From: Jan Grewe Date: Wed, 18 Oct 2017 11:24:23 +0200 Subject: [PATCH] r=translate to english, remove matrix exercises --- programming/exercises/vectors_matrices.tex | 410 +++++---------------- 1 file changed, 100 insertions(+), 310 deletions(-) diff --git a/programming/exercises/vectors_matrices.tex b/programming/exercises/vectors_matrices.tex index 16af545..2e5ca46 100644 --- a/programming/exercises/vectors_matrices.tex +++ b/programming/exercises/vectors_matrices.tex @@ -13,10 +13,11 @@ %%%%% text size %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \usepackage[left=20mm,right=20mm,top=25mm,bottom=25mm]{geometry} -\pagestyle{headandfoot} \header{{\bfseries\large \"Ubung - 2}}{{\bfseries\large Vektoren und Matrizen}}{{\bfseries\large 12. Oktober, 2015}} +\pagestyle{headandfoot} +\header{{\bfseries\large Exercise 2}}{{\bfseries\large Vectors}}{{\bfseries\large 18. Oktober, 2017}} \firstpagefooter{Dr. Jan Grewe}{Phone: 29 74588}{Email: - jan.grewe@uni-tuebingen.de} \runningfooter{}{\thepage}{} + jan.grewe@uni-tuebingen.de} +\runningfooter{}{\thepage}{} \setlength{\baselineskip}{15pt} \setlength{\parindent}{0.0cm} @@ -24,387 +25,176 @@ \renewcommand{\baselinestretch}{1.15} \newcommand{\code}[1]{\texttt{#1}} -\renewcommand{\solutiontitle}{\noindent\textbf{L\"osung:}\par\noindent} +\renewcommand{\solutiontitle}{\noindent\textbf{Solutions:}\par\noindent} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \begin{document} - \vspace*{-6.5ex} \begin{center} - \textbf{\Large Einf\"uhrung in die wissenschaftliche Datenverarbeitung}\\[1ex] - {\large Jan Grewe, Jan Benda}\\[-3ex] - Abteilung Neuroethologie \hfill --- \hfill Institut f\"ur Neurobiologie \hfill --- \hfill \includegraphics[width=0.28\textwidth]{UT_WBMW_Black_RGB} \\ + \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} -Die folgenden Aufgaben dienen der Wiederholung, \"Ubung und -Selbstkontrolle und sollten eigenst\"andig bearbeitet und gel\"ost -werden. Die L\"osung soll in Form eines einzelnen Skriptes (m-files) -im ILIAS hochgeladen werden. Jede Aufgabe sollte in einer eigenen -``Zelle'' gel\"ost sein. Die Zellen \textbf{m\"ussen} unabh\"angig -voneinander ausf\"uhrbar sein. Das Skript sollte nach dem Muster: -\linebreak ``vektoren\_matrizen\_\{nachname\}.m'' benannt werden -(z.B. vektoren\_matrizen\_mueller.m). +The exercises are meant for self-monitoring and 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} - \section*{Vektoren} - \question Erzeuge Vektoren mit folgendem Inhalt: - \begin{parts} - \part Von 1 bis 10 in ganzzahligen Schritten. - \begin{solution} - \code{a = 1:10;} - \end{solution} - \part Von 0 bis 20 in 2er Schritten. - \begin{solution} - \code{a = 0:2:20;} - \end{solution} - \part Mit \textbf{absteigendem} Inhalt von 100 bis 0. - \begin{solution} - \code{a = 100:-1:0;} - \end{solution} - \part In 10 Schritten von 0 bis 1. - \begin{solution} - \code{a = 0:0.1:1;} - \end{solution} - \part In 11 Schritten von 0 bis 1. - \begin{solution} - \code{a = 0:1/11:1;} - \end{solution} - \part In 50 Schritten von 0 bis $2\pi$ ($\pi$ ist als Konstante - \code{pi} in Matlab definiert). - \begin{solution} - \code{a = 0:2*pi/50:2*pi;} - \end{solution} - \end{parts} - - \question Rechnen mit Vektoren: - \begin{parts} - \part Definiere einen Vektor \code{x = [3 2 6 8];} - \part Wie gro{\ss} ist der Vektor? Benutze die Funktionen - \code{size} und \code{length}. Was ist der Unterschied zwischen - den beiden Funktionen? - \begin{solution} - \code{x = [3 2 6 8]; \\ disp(length(x));\\ 4\\ disp(size(x))\\ 1 4} - \end{solution} - \part Wie \"andern sich \code{size} und \code{length} des - Vektors wenn er transponiert wird? - \begin{solution} - L\"ange \"andert sich nicht. R\"uckgabewert von size ist invertiert. - \end{solution} - \part Addiere 5 zu jedem Element von \verb+x+. - \begin{solution} - \code{disp(x + 5)} - \end{solution} - \part Multipliziere jedes Element von \code{x} mit 2; - \begin{solution} - \code{disp(x * 2)} - \end{solution} - \part Definiere einen zweiten Vektor (\verb+y = [4 1 3 5];+). - Stelle sicher, dass \code{x} wieder in seiner urspr\"unglichen - Form ist. - \part Addiere beide Vektoren \code{x + y}. - \begin{solution} - \code{y = [4 1 3 5]; \\disp(x + y)\\7 3 9 13} - \end{solution} - \part Subtrahiere beide Vektoren \code{x - y}. - \begin{solution} - \code{disp(x - y)\\-1 1 3 3} - \end{solution} - \part Multipliziere beide Vektoren \code{x * y}. - \begin{solution} - \code{disp(x * y)\\Error using *. Inner matrix dimension must agree.} - \end{solution} - \part Erkl\"are die Fehlermeldung. - \begin{solution} - * ist der Operator f\"ur die Matrixmultiplikation. Bei dieser - muessen die inneren Dimensionen \"uebereinstimmen.\linebreak - \code{disp(size(x))\\1 4 \\disp(size(y)) \\ 1 4}\\ - (m,n)*(n,o) w\"are ok. - \end{solution} - \part Was m\"usste man machen, damit \code{mtimes} bzw. der - \code{*} Operator funktionieren? - \begin{solution} - y m\"usste transponiert werden: \code{x * y'} - \end{solution} - \part Multipliziere die Vektoren elementweise (\code{x .* y}) - und weise das Ergebnis eine neuen Variablen zu. - \begin{solution} - \code{z = x .* y;} - \end{solution} - \end{parts} - - \question Erzeugen von Vektoren mit Helferfunktionen: - \begin{parts} - \part Erstelle einen 100 Elemente langen Vektor mit der Funktion - \code{ones} (siehe Hilfe). Was macht sie? - \begin{solution} - \code{ones(100,1)} erzeugt einen Vektor bei dem alle Elemente mit 1 gef\"ullt sind. - \end{solution} - \part Erstelle einen 100 Elemente langen Vektor mit der Funktion - \code{zeros}. Was macht diese? - \begin{solution} - \code{zeros(100,1)} erzeugt einen Vektor bei dem alle Elemente mit 0 gef\"ullt sind. - \end{solution} - \part Erstelle einen 100 Elemente langen Vektor in dem jedes - Element den Wert 4.5 hat. - \begin{solution} - \code{ones(100,1) * 4.5} - \end{solution} - \part Erzeuge einen Vektor mit 100 Zufallszahlen (\code{rand}, - siehe Hilfe). - \begin{solution} - \code{x = rand(100,1)} - \end{solution} - \part Erzeuge einen Vektor mit 100 Werten zwischen 0 und 1 - mithilfe der Funktion \code{linspace}. - \begin{solution} - \code{x = linspace(0,1,100)} - \end{solution} - \end{parts} - - \question Indizieren in Vektoren: - \begin{parts} - \part Erzeuge einen Vektor mit 100 Elementen (0 - 100). - \begin{solution} - \code{x = linspace(0,100,100);} - \end{solution} - \part Gib jeweils den ersten, den letzten, den 5., 24. und den - vorletzten Wert aus. - \begin{solution} - \code{disp(x(1))\\ disp(x(end))\\ disp(x(5))\\ disp(x(24))\\ disp(x(end-1))} - \end{solution} - \part Gib die ersten 10 Werte aus. - \begin{solution} - \code{x(1:10)} - \end{solution} - \part Gib die letzten 10 Werte aus. - \begin{solution} - \code{disp(x(end-9:end))} - \end{solution} - \part Versuche den Wert an der Stelle 0 auszugeben. - \begin{solution} - \code{x(0)\\ Subscript indices must either be real positive integers or logicals.} - \end{solution} - \part Versuche den Wert an der Stelle 110 auszugeben. - \begin{solution} - \code{x(110)\\ Index exceeds matrix dimensions.} - \end{solution} - \part Gib die Werte an den Stellen 3, 15, und 42 zusammen als - Vektor aus. - \begin{solution} - \code{disp(x([3 15 42]))} - \end{solution} - \part Gib 10 zuf\"allig ausgew\"ahlte Werte aus (benutze - \verb+randi+ um die Indizes zu erstellen). - \begin{solution} - \code{x(randi(100,10,1))} - \end{solution} - \end{parts} - - \question Erzeuge eine Variable und speichere etwas Text in ihr, - so dass mindestens 2 Worte vorhanden sind. (z.B. \code{x = 'some - text'}). Benutze die Indizierung um die W\"orter einzeln - auszugeben. - \begin{solution} - \code{x = 'some text'; \\ disp(x(1:4))\\disp(x(6:end))} - \end{solution} - - - \newpage - \section*{Matrizen} - - \question Erstelle folgende Matrix - \[ A = \left( \begin{array}{ccc} 7 & 3 & 5 \\ 1 & 8 & 3 \\ 8 & 6 & - 4 \end{array} \right) \] + \question Create vector with the following contents: \begin{parts} - \part Benutze die Funktion \code{size} um die Gr\"o{\ss}e vpm \code{A} anzeeigen zu lassen. + \part Integer numbers ranging from 1 to 10. \begin{solution} - \code{x = [7 3 5; 1 8 3; 8 6 4];\\disp(size(x))} + \code{a = 1:10;} \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. + \part Integer numbers in the range 0 to 20 in steps of 2. \begin{solution} - \code{disp(size(x, 1))}\\\code{disp(size(x, 2))} + \code{a = 0:2:20;} \end{solution} - \part Gib das Element in der 3. Zeile und 2. Spalte aus. + \part \textbf{Descending} values ranging from 100 to 0. \begin{solution} - \code{x(3,2)} + \code{a = 100:-1:0;} \end{solution} - \part Gib jeweils alle Elemente der 1., 2. und 3. Zeile aus. + \part In 10 steps from 0 to 1. \begin{solution} - \code{disp(x([1 2 3],:));} + \code{a = 0:0.1:1;} \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. + \part In 11 steps from 0 to 1. \begin{solution} - \code{x(1,:) = x(1,:) - 5;} + \code{a = 0:1/11:1;} \end{solution} - \part Multipliziere alle Elementen der 3. Spalte mit 2. + \part In 50 steps from 0 to $2\pi$ ($\pi$ is known in Matlab as the constant + \code{pi}). \begin{solution} - \code{x(:,3) = x(:,3) * 2;} + \code{a = 0:2*pi/50:2*pi;} \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?). + \question Calculations with vectors: \begin{parts} - \part Gib das Element in der 2. Zeile und 3. Spalte aus. + \part Create a vector \code{x = [3 2 6 8];} + \part What is the size of this vector? Use the functions \code{size} and \code{length}. What is the difference between them? \begin{solution} - \code{M = randn(5, 5);} - \code{disp(M(2,3))} + \code{x = [3 2 6 8]; + \\ disp(length(x));\\ 4\\ disp(size(x))\\ 1 4} \end{solution} - - \part Gib jeweils alle Elemente der 1., 3. und letzten Zeile aus. + \part What changes in \code{size} and \code{length} when you transpose the vector. \begin{solution} - \code{disp(M(1,:)) \\ disp(M(3,:))\\ disp(M(size(M,1), :))} + The length does not change, the size is inverted. \end{solution} - - \part Gib jeweils alle Elemente der 2. und 4. Spalte aus. + \part Add 5 to each element of \verb+x+. \begin{solution} - \code{disp(M(:,2))\\ disp(M(:,4))} + \code{disp(x + 5)} \end{solution} - - \part Greife mit einem einzigen Kommando auf die Elemente jeder - zweiten Spalte zu und speichere die Daten in einer neuen Variable. + \part Multiply each element of \code{x} with 2; \begin{solution} - \code{y = M(:, [2:2:size(M,2)])} + \code{disp(x * 2)} \end{solution} - - \part Berechne jeweils den Mittelwert der 1., 3. und 5. Zeile - (Funktion \code{mean}, siehe Hilfe). + \part Create a second vector (\verb+y = [4 1 3 5];+). + Make sure that \code{x} is in its original form. + \part Add both vectors \code{x + y}. \begin{solution} - \code{mean(M([1 3 5],:), 2)} + \code{y = [4 1 3 5]; \\disp(x + y)\\7 3 9 13} \end{solution} - - \part Berechne die Summe aller Werte der 2. und 4. Spalte - (Funktion \code{sum}, siehe Hilfe). + \part Subtract \code{y} from \code{x}. \begin{solution} - \code{sum(M(:, [2 4]), 1)} + \code{disp(x - y)\\-1 1 3 3} \end{solution} - - \part Berechne die Summe aller Elemente der Matrize. + \part Multiply both vectors \code{x * y}. \begin{solution} - \code{sum(M(:))} + \code{disp(x * y)\\Error using *. Inner matrix dimension must agree.} \end{solution} - - \part Ersetze die Elemente der 2. Zeile mit denen der 4. + \part Explain the error message \begin{solution} - \code{M(2,:) = M(4,:)} + * operator is the matrix multiplication. The inner dimensions must agree.\linebreak + \code{disp(size(x))\\1 4 \\disp(size(y)) \\ 1 4}\\ + (m,n)*(n,o) w\"are ok. \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? + \part What needs to be done to make \code{mtimes} and + \code{*} working? \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; + y needs to be transposed: \code{x * y'} + \end{solution} + \part Multiply element-wise (\code{x .* y}) and assign the result to a new variable. + \begin{solution} + \code{z = x .* y;} \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+). + \question Creating vectors using helper functions: \begin{parts} - \part Erstelle eine 2-D Matrix mit Zufallszahlen mit der Dimensionalit\"at - \verb+[10,10]+. + \part Create a vector of the length 100 using the function + \code{ones} (see help). What is its purpose? \begin{solution} - \code{x = randn(10, 10)} + \code{ones(100,1)} creates a vector of the given size and fills it with 1. \end{solution} - - \part Wie viele Werte enth\"alt sie? + \part Create a vector of the length 100 using the function + \code{zeros} (see help). What is its purpose? \begin{solution} - \code{disp(numel(x))} + \code{zeros(100,1)} creates a vector of the given size and fills it with 0. \end{solution} - - \part Benutze das lineare Indizieren um 50 zuf\"allige Werte - auszuw\"ahlen. + \part Create a vector with 100 elements. All elements should have the value + 4.5. \begin{solution} - \code{x(randi(100, 50, 1)])} + \code{ones(100, 1) * 4.5} \end{solution} - - \part Wo liegt der Vorteil gegen\"uber der \textit{subscript} - Indizierung? + \part Create a 100 element vector filled with random numbers (\code{rand}, + see help). \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. + \code{x = rand(100, 1)} \end{solution} - \part Berechne die Summe aller Werte mit einem Funktionsaufruf.. + \part Use the function \code{linspace} to create a 100 element vector with values between 0 and 1. \begin{solution} - \code{sum(x(:))} oder \code{sum(sum(x))} + \code{x = linspace(0, 1, 100)} \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. + + \question Indexing in vectors: \begin{parts} - \part \code{x + y} + \part Create a 100 element length vector with values ranging from 0 to 99. \begin{solution} - Funktioniert! + \code{x = linspace(0, 99, 100);} \end{solution} - - \part \code{x * M} + \part Print the first, last, fifth, 24th and the second-to-last value. \begin{solution} - Matrixmultiplikation Funktioniert nicht! Inner dimensions must agree! + \code{disp(x(1))\\ disp(x(end))\\ disp(x(5))\\ disp(x(24))\\ disp(x(end-1))} \end{solution} - - \part \code{x + y'} + \part Print the first 10 values. \begin{solution} - Funktioniert nicht! Die Dimensionalit\"aten passen nicht. + \code{x(1:10)} \end{solution} - - \part \code{M - [x y]} + \part Print the last 10 values. \begin{solution} - Funktioniert nicht! \code{[x y] ist ein Zeilenvektor der L\"ange - 6, M ist eine Martix.} + \code{disp(x(end-9:end))} \end{solution} - - \part \code{[x; y]} + \part Try to print the value at the zeroth position. \begin{solution} - Funktioniert! Gr\"o{\ss}e: 2 3 + \code{x(0)\\ Subscript indices must either be real positive integers or logicals.} \end{solution} - - \part \code{M - [x; y]} + \part Try to access the value at the 110th position. \begin{solution} - Funktioniert! + \code{x(110)\\ Index exceeds matrix dimensions.} \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). + \part Access the values at the positions 3, 15, and 42 with a single command. \begin{solution} - \code{x = randn(5,5); \\y = randn(5, 5);\\ z = cat(3, x, y);\\disp(z(:,:,1))} + \code{disp(x([3 15 42]))} \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). + \part Access 10 randomly selected values (used \verb+randi+ to create random indices). \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))))} + \code{x(randi(100,10,1))} \end{solution} \end{parts} - \end{questions} + + \question Store some text in a valriable. The text should consist of at least two words (e.g. \code{x = 'some + text'}). Use indexing to print out the words individually. + \begin{solution} + \code{x = 'some text'; \\ disp(x(1:4))\\disp(x(6:end))} + \end{solution} + +\end{questions} \end{document}