<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">\documentclass[12pt,a4paper,pdftex]{exam}

\newcommand{\exercisetopic}{Vectors}
\newcommand{\exercisenum}{2}
\newcommand{\exercisedate}{3. November, 2020}

\input{../../exercisesheader}

\firstpagefooter{Dr. Jan Grewe}{}{jan.grewe@uni-tuebingen.de}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}

\input{../../exercisestitle}

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}
  \question Create vector with the following contents:
  \begin{parts}
    \part Integer numbers ranging from 1 to 10.
    \begin{solution}
      \code{a = 1:10;}
    \end{solution}
    \part Integer numbers in the range 0 to 20 in steps of 2.
    \begin{solution}
      \code{a = 0:2:20;}
    \end{solution}
    \part \textbf{Descending} values ranging from 100 to 0.
    \begin{solution}
      \code{a = 100:-1:0;}
    \end{solution}
    \part In 10 steps from 0 to 1.
    \begin{solution}
      \code{a = 0:0.1:1;}
    \end{solution}
    \part In 11 steps from 0 to 1.
    \begin{solution}
      \code{a = 0:1/11:1;}
    \end{solution}
    \part In 50 steps from 0 to $2\pi$ ($\pi$ is known in Matlab as the constant
    \code{pi}).
    \begin{solution}
      \code{a = 0:2*pi/50:2*pi;}
    \end{solution}
  \end{parts}

  \question Calculations with vectors:
  \begin{parts}
    \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{x = [3 2 6 8];
        \\ disp(length(x));\\ 4\\ disp(size(x))\\ 1 4}
    \end{solution}
    \part What changes in \code{size} and \code{length} when you transpose the vector.
    \begin{solution}
      The length does not change, the size is inverted.
    \end{solution}
    \part Add 5 to each element of \verb+x+.
    \begin{solution}
      \code{disp(x + 5)}
    \end{solution}
    \part Multiply each element of \code{x} with 2;
    \begin{solution}
      \code{disp(x * 2)}
    \end{solution}
    \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{y = [4 1 3 5]; \\disp(x + y)\\7     3     9    13}
    \end{solution}
    \part Subtract \code{y} from \code{x}.
    \begin{solution}
      \code{disp(x - y)\\-1     1     3    3}
    \end{solution}
    \part Multiply both vectors \code{x * y}.
    \begin{solution}
      \code{disp(x * y)\\Error using *. Inner matrix dimension must agree.}
    \end{solution}
    \part Explain the error message
    \begin{solution}
      * 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 What needs to be done to make \code{mtimes} and 
    \code{*} working?
    \begin{solution}
      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 Creating vectors using helper functions:
  \begin{parts}
    \part Create a vector of the length 100 using the function
    \code{ones} (see help). What is its purpose?
    \begin{solution}
      \code{ones(100,1)} creates a vector of the given size and fills it with 1.
    \end{solution}
    \part Create a vector of the length 100 using the function
    \code{zeros} (see help). What is its purpose?
    \begin{solution}
      \code{zeros(100,1)} creates a vector of the given size and fills it with 0.
    \end{solution}
    \part Create a vector with 100 elements. All elements should have the value
    4.5.
    \begin{solution}
      \code{ones(100, 1) * 4.5}
    \end{solution}
    \part Create a 100 element vector filled with random numbers (\code{rand},
    see help).
    \begin{solution}
      \code{x = rand(100, 1)}
    \end{solution}
    \part Use the function \code{linspace} to create a 100 element vector with values between 0 and 1.
    \begin{solution}
      \code{x = linspace(0, 1, 100)}
    \end{solution}
  \end{parts}

  \question Indexing in vectors:
  \begin{parts}
    \part Create a 100 element length vector with values ranging from 0 to 99.
    \begin{solution}
      \code{x = linspace(0, 99, 100);}
    \end{solution}
    \part Print the first, last, fifth, 24th and the second-to-last value.
    \begin{solution}
      \code{disp(x(1))\\ disp(x(end))\\ disp(x(5))\\ disp(x(24))\\ disp(x(end-1))}
    \end{solution}
    \part Print the first 10 values.
    \begin{solution}
      \code{x(1:10)}
    \end{solution}
    \part Print the last 10 values.
    \begin{solution}
      \code{disp(x(end-9:end))}
    \end{solution}
    \part Try to print the value at the zeroth position.
    \begin{solution}
      \code{x(0)\\ Subscript indices must either be real positive integers or logicals.}
    \end{solution}
    \part Try to access the value at the 110th position.
    \begin{solution}
      \code{x(110)\\ Index exceeds matrix dimensions.}
    \end{solution}
    \part Access the values at the positions 3, 15, and 42 with a single command.
    \begin{solution}
      \code{disp(x([3 15 42]))}
    \end{solution}
    \part Access 10 randomly selected values (used \verb+randi+ to create random indices).
    \begin{solution}
      \code{x(randi(100,10,1))}
    \end{solution}
  \end{parts}

  \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}
</pre></body></html>