234 lines
8.0 KiB
TeX
234 lines
8.0 KiB
TeX
\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 22. Oktober, 2019}}
|
|
\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 and revision of the
|
|
lecture. 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:
|
|
``matrices\_\{lastname\}.m''(e.g. matrices\_mueller.m).
|
|
|
|
\begin{questions}
|
|
|
|
\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 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 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 Copy the content at the position 3rd line, 2nd column to a new variable.
|
|
\begin{solution}
|
|
\code{c = x(3, 2)}
|
|
\end{solution}
|
|
\part Display all elements of the 1st, 2nd and 3rd line.
|
|
\begin{solution}
|
|
\code{disp(x([1 2 3], :));}
|
|
\end{solution}
|
|
\part Display all elements of the 1st, 2nd, and 3rd column.
|
|
\begin{solution}
|
|
\code{disp(x(:, 1))\\ disp(x(:, 2))\\ disp(x(:, 3))}
|
|
\end{solution}
|
|
\part Increment all elements of the 2nd line and the 3rd column about 1 (reassign the result to the respective elements).
|
|
\begin{solution}
|
|
\code{x(2,:) = x(2,:) + 1;}\\
|
|
\code{x(:,3) = x(:,3) + 1;}
|
|
\end{solution}
|
|
\part Subtract five from all elements of the 1st line.
|
|
\begin{solution}
|
|
\code{x(1,:) = x(1,:) - 5;}
|
|
\end{solution}
|
|
\part Multiply all elements of the 3rd column by 2.
|
|
\begin{solution}
|
|
\code{x(:,3) = x(:,3) .* 2;}
|
|
\end{solution}
|
|
\end{parts}
|
|
|
|
\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 Display the content of \code{M} at position 2nd line and 3rd column.
|
|
\begin{solution}
|
|
\code{M = randn(5, 5);}
|
|
\code{disp(M(2,3))}
|
|
\end{solution}
|
|
|
|
\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 Print the elements of the 2nd and 4th column.
|
|
\begin{solution}
|
|
\code{disp(M(:,2))\\ disp(M(:,4))}
|
|
\end{solution}
|
|
|
|
\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 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 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 Calculate the total sum of all elements in \code{M}
|
|
\begin{solution}
|
|
\code{sum(M(:))}
|
|
\end{solution}
|
|
|
|
\part Replace all elements of the 2nd line with those of the 4th line.
|
|
\begin{solution}
|
|
\code{M(2,:) = M(4,:)}
|
|
\end{solution}
|
|
|
|
\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
|
|
Auswahl von M in die geschrieben werden soll hat nur die
|
|
Gr\"o{\ss}e 2;
|
|
\end{solution}
|
|
\end{parts}
|
|
|
|
\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 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 How many elements are stored in it?
|
|
\begin{solution}
|
|
\code{disp(numel(x))}
|
|
\end{solution}
|
|
|
|
\part Employ linar indexing to select 50 random values.
|
|
\begin{solution}
|
|
\code{x(randi(100, 50, 1)])}
|
|
\end{solution}
|
|
|
|
\part Can you imaging an advantage of using linear indexing instead of subscript indexing?
|
|
\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 Calculate the total sum of all elements with a single command.
|
|
\begin{solution}
|
|
\code{sum(x(:))} or \code{sum(sum(x))}
|
|
\end{solution}
|
|
|
|
\end{parts}
|
|
|
|
\question Create 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 fail? If it fails,
|
|
why? Test your predictions.
|
|
\begin{parts}
|
|
\part \code{x + y}
|
|
\begin{solution}
|
|
works!
|
|
\end{solution}
|
|
|
|
\part \code{x * M}
|
|
\begin{solution}
|
|
Matrixmultiplication will not work! Inner dimensions must agree!
|
|
\end{solution}
|
|
|
|
\part \code{x + y'}
|
|
\begin{solution}
|
|
Fail! Dimensionalities do not match.
|
|
\end{solution}
|
|
|
|
\part \code{M - [x y]}
|
|
\begin{solution}
|
|
Fail! \code{[x y] is a line vector of length 6, M is a martix.}
|
|
\end{solution}
|
|
|
|
\part \code{[x; y]}
|
|
\begin{solution}
|
|
Works! Size: 2 3
|
|
\end{solution}
|
|
|
|
\part \code{M - [x; y]}
|
|
\begin{solution}
|
|
Works!
|
|
\end{solution}
|
|
\end{parts}
|
|
|
|
\question Create a 3-D matrix from two 2-D matrices. Use the
|
|
function \code{cat} (check the help to learn its usage).
|
|
\begin{parts}
|
|
\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 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 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}
|
|
\end{parts}
|
|
\end{questions}
|
|
|
|
\end{document}
|