From 99fac2be070d5e6f414f926069959ebd5ae15a37 Mon Sep 17 00:00:00 2001 From: Jan Grewe Date: Tue, 24 Oct 2017 00:00:56 +0200 Subject: [PATCH] [exercises] matric exerciese translated --- programming/exercises/matrices.tex | 111 +++++++++++++---------------- 1 file changed, 48 insertions(+), 63 deletions(-) diff --git a/programming/exercises/matrices.tex b/programming/exercises/matrices.tex index ad4d0ce..dd7c08c 100644 --- a/programming/exercises/matrices.tex +++ b/programming/exercises/matrices.tex @@ -14,7 +14,7 @@ %%%%% 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}} +\header{{\bfseries\large Exercise 3}}{{\bfseries\large Matrices}}{{\bfseries\large 23. Oktober, 2017}} \firstpagefooter{Dr. Jan Grewe}{Phone: 29 74588}{Email: jan.grewe@uni-tuebingen.de} \runningfooter{}{\thepage}{} @@ -46,95 +46,91 @@ executable on its own. The file should be named according to the following patte \begin{questions} - \question Erstelle folgende Matrix + \question Create the following 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. + \part Use the function \code{size} to check for its size. \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. + \part Use the help to figure out how to get only the size along a certain axis. Print the sizes of each dimension. \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. + \part Retrieve the element at the position 3rd line, 2nd column. \begin{solution} \code{x(3,2)} \end{solution} - \part Gib jeweils alle Elemente der 1., 2. und 3. Zeile aus. + \part Print all elements of the 1st, 2nd and 3rd line. \begin{solution} \code{disp(x([1 2 3],:));} \end{solution} - \part Gib jeweils alle Elemente der 1., 2., und 3. Spalte aus. + \part Print all elements of the 1st, 2nd, and 3rd column. \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. + \part Increment all elements of the 2nd line and the 3rd column about 1. \begin{solution} \code{x(2,3) = x(2,3) + 1;} \end{solution} - \part Ziehe von allen Elementen der 1. Zeile 5 ab. + \part Subtract five from all elements of the 1st line. \begin{solution} \code{x(1,:) = x(1,:) - 5;} \end{solution} - \part Multipliziere alle Elementen der 3. Spalte mit 2. + \part Multiply all elements of the 3rd column with 2. \begin{solution} - \code{x(:,3) = x(:,3) * 2;} + \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?). + \question Create a $5 \times 5$ matrix \code{M} that contains random numbers (use the function + \verb+randn()+. Use the help to find out what it does). \begin{parts} - \part Gib das Element in der 2. Zeile und 3. Spalte aus. + \part Print the element at the position 2nd line and 3rd column. \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. + + \part Print all elements of the 1st, 3rd and last line. \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. + \part Print the elements of the 2nd and 4th column. \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. + \part Select with a single command all elements of every 2nd column and store them in a new 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). + \part Calculate the averages of lines 1, 3, and 5 (use the function mean, see help). \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). + \part Calculate the sum of all elements in the 2nd and 4th column + (function \code{sum}, see help). \begin{solution} \code{sum(M(:, [2 4]), 1)} \end{solution} - \part Berechne die Summe aller Elemente der Matrize. + \part Calculate the total sum of all elements in \code{M} \begin{solution} \code{sum(M(:))} \end{solution} - \part Ersetze die Elemente der 2. Zeile mit denen der 4. + \part Exchange all elements of the 2nd with those of the 4th line. \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? + \part Execute the following command: \code{M(1:2,1) = [1, 2, + 3]}. What could have been intended by the command and what does the error message tell? \begin{solution} \code{M(1:2,1) = [1, 2,3];\\ Subscripted assignment dimension mismatch.} \\ Der einzuf\"ugende Vektor hat 3 Elemente, die @@ -143,30 +139,26 @@ executable on its own. The file should be named according to the following patte \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 Indexing in matrices can use the + \textit{subscript} indices or the \textit{linear} indices (you may want to check the help for the \verb+sub2ind+ and \verb+ind2sub+). \begin{parts} - \part Erstelle eine 2-D Matrix mit Zufallszahlen mit der Dimensionalit\"at + \part Create a 2-D matric filled with random numbers and the dimensionality \verb+[10,10]+. \begin{solution} \code{x = randn(10, 10)} \end{solution} - \part Wie viele Werte enth\"alt sie? + \part How many elements are stored in it? \begin{solution} \code{disp(numel(x))} \end{solution} - \part Benutze das lineare Indizieren um 50 zuf\"allige Werte - auszuw\"ahlen. + \part Employ linar indexing to select 50 random values. \begin{solution} \code{x(randi(100, 50, 1)])} \end{solution} - \part Wo liegt der Vorteil gegen\"uber der \textit{subscript} - Indizierung? + \part Is there an advantage to use the linear indexing? \begin{solution} Die Matrize ist 2-dimensional. Wenn mit dem subscript index zugegriffen werden soll, dann muss auf die Dimensionen @@ -175,66 +167,59 @@ executable on its own. The file should be named according to the following patte 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.. + + \part Calculate the total sum of all elements with a single command. \begin{solution} - \code{sum(x(:))} oder \code{sum(sum(x))} + \code{sum(x(:))} or \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. + \question Create the three variables \verb+x = [1 5 9]+ and + \verb+y = [7 1 5]+ and \verb+M = [3 1 6; 5 2 7]+. Which of the following commands will pass? Which command will not? If not, why? Test your predictions. \begin{parts} \part \code{x + y} \begin{solution} - Funktioniert! + works! \end{solution} \part \code{x * M} \begin{solution} - Matrixmultiplikation Funktioniert nicht! Inner dimensions must agree! + Matrixmultiplication will not work! Inner dimensions must agree! \end{solution} \part \code{x + y'} \begin{solution} - Funktioniert nicht! Die Dimensionalit\"aten passen nicht. + Fail! Dimensionalities do not match. \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.} + Fail! \code{[x y] is a line vector of length 6, M is a martix.} \end{solution} \part \code{[x; y]} \begin{solution} - Funktioniert! Gr\"o{\ss}e: 2 3 + Works! Size: 2 3 \end{solution} \part \code{M - [x; y]} \begin{solution} - Funktioniert! + Works! \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). + + \question Create a 3-D matrix from two 2-D matrices. Use the function cat (check the help to learn its usage). \begin{parts} - \part Gib alle Elemente des ersten ``Blattes'' aus (Index 1 der 3. Dimension). + \part Select all elements of the first ``page'' (index 1, 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. + + \question Create a $5 \times 5 \times 5$ matrix of random numbers that have been drawn from a uniform distribution. Values should be in the range 0 and 100. \begin{parts} - \part Berechne den Mittelwert aller Bl\"atter dieser Matrix - (benutze \verb+mean()+, siehe Hilfe). + \part Calculate the average of each ``page'' (function \verb+mean()+, see help). \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}