[programming] language fixes in chapter and exercises
This commit is contained in:
parent
55b3451152
commit
a518043a02
@ -54,7 +54,7 @@ following pattern:
|
||||
\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.
|
||||
\part Use the help to figure out how to get only the size along a certain dimension. Display the sizes of each dimension.
|
||||
\begin{solution}
|
||||
\code{disp(size(x, 1))}\\\code{disp(size(x, 2))}
|
||||
\end{solution}
|
||||
@ -94,12 +94,12 @@ following pattern:
|
||||
\code{disp(M(2,3))}
|
||||
\end{solution}
|
||||
|
||||
\part Print all elements of the 1st, 3rd and last line.
|
||||
\part Display 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.
|
||||
\part Display the elements of the 2nd and 4th column.
|
||||
\begin{solution}
|
||||
\code{disp(M(:,2))\\ disp(M(:,4))}
|
||||
\end{solution}
|
||||
@ -109,7 +109,7 @@ following pattern:
|
||||
\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).
|
||||
\part Calculate the averages of lines 1, 3, and 5 (use the function \verb+mean+}, see help).
|
||||
\begin{solution}
|
||||
\code{mean(M([1 3 5],:), 2)}
|
||||
\end{solution}
|
||||
@ -141,9 +141,9 @@ following pattern:
|
||||
\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+).
|
||||
\textit{subscript} indices or the \textit{linear} indices (you may want to check the help for the functions \verb+sub2ind+ and \verb+ind2sub+).
|
||||
\begin{parts}
|
||||
\part Create a 2-D matric filled with random numbers and the dimensionality
|
||||
\part Create a 2-D matrix filled with random numbers and the size
|
||||
\verb+[10,10]+.
|
||||
\begin{solution}
|
||||
\code{x = randn(10, 10)}
|
||||
@ -154,12 +154,12 @@ following pattern:
|
||||
\code{disp(numel(x))}
|
||||
\end{solution}
|
||||
|
||||
\part Employ linar indexing to select 50 random values.
|
||||
\part Employ linear 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?
|
||||
\part Can you imagine 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
|
||||
@ -225,7 +225,7 @@ following pattern:
|
||||
\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))))}
|
||||
\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}
|
||||
|
@ -95,7 +95,7 @@ following pattern: ``variables\_datatypes\_\{lastname\}.m''
|
||||
\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.
|
||||
Make sure that \code{x} is in its original form (see (a)).
|
||||
\part Add both vectors \code{x + y}.
|
||||
\begin{solution}
|
||||
\code{y = [4 1 3 5]; \\disp(x + y)\\7 3 9 13}
|
||||
@ -155,23 +155,23 @@ following pattern: ``variables\_datatypes\_\{lastname\}.m''
|
||||
|
||||
\question Indexing in vectors:
|
||||
\begin{parts}
|
||||
\part Create a 100 element length vector with values ranging from 0 to 99.
|
||||
\part Create a vector of the length 100 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.
|
||||
\part use \code{disp()) to display the first, last, fifth, 24th and the second-to-last value on the command line.
|
||||
\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.
|
||||
\part Display the first 10 values.
|
||||
\begin{solution}
|
||||
\code{x(1:10)}
|
||||
\end{solution}
|
||||
\part Print the last 10 values.
|
||||
\part Display the last 10 values.
|
||||
\begin{solution}
|
||||
\code{disp(x(end-9:end))}
|
||||
\end{solution}
|
||||
\part Try to print the value at the zeroth position.
|
||||
\part Try to display the value at the zeroth position.
|
||||
\begin{solution}
|
||||
\code{x(0)\\ Subscript indices must either be real positive integers or logicals.}
|
||||
\end{solution}
|
||||
@ -179,18 +179,18 @@ following pattern: ``variables\_datatypes\_\{lastname\}.m''
|
||||
\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.
|
||||
\part Access and display 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).
|
||||
\part Access and display 10 randomly selected values (used \verb+randi+ to create random indices).
|
||||
\begin{solution}
|
||||
\code{x(randi(100, 10, 1))}
|
||||
\code{disp(x(randi(100, 10, 1)))}
|
||||
\end{solution}
|
||||
\end{parts}
|
||||
|
||||
\question Store some text in a variable. The text should consist of at least two words (e.g. \code{x = 'some
|
||||
text'}). Use indexing to print out the words individually.
|
||||
text'}). Use indexing to display the words individually.
|
||||
\begin{solution}
|
||||
\code{x = 'some text'; \\ disp(x(1:4))\\disp(x(6:end))}
|
||||
\end{solution}
|
||||
|
@ -254,7 +254,7 @@ type (figure~\ref{vectorfig} B). The variable \varcode{test} in
|
||||
\begin{figure}[ht]
|
||||
\includegraphics[width=0.8\columnwidth]{scalarArray}
|
||||
\titlecaption{Scalars and vectors.}{\textbf{A)} A scalar variable
|
||||
holds exactly on value. \textbf{B)} A vector can hold multiple
|
||||
holds exactly one value. \textbf{B)} A vector can hold multiple
|
||||
values. These must be of the same data type (e.g. integer
|
||||
numbers). \matlab{} distinguishes between row- and
|
||||
column-vectors.}\label{vectorfig}
|
||||
@ -646,7 +646,7 @@ and should be always preferred over \code{length()}.
|
||||
|
||||
Analogous to the data access in vectors we can address individual
|
||||
elements of a matrix by it's index. Similar to a coordinate system
|
||||
each element is addressed using a n-tuple with $n$ the number of
|
||||
each element is addressed using an n-tuple with $n$ the number of
|
||||
dimensions (figure~\ref{matrixindexingfig},
|
||||
listing~\ref{matrixIndexing}). This type of indexing is called
|
||||
\codeterm{subscript indexing}. The first coordinate refers always to
|
||||
@ -910,10 +910,10 @@ this can save processing time.
|
||||
|
||||
Previously we have introduced the data types for integer or floating
|
||||
point numbers and discussed that there are instances in which it is
|
||||
more efficient to use a integer data type rather than storing floating
|
||||
more efficient to use an integer data type rather than storing floating
|
||||
point numbers. The result of a Boolean expression can only assume two
|
||||
values (true or false). This implies that we need only a single bit to
|
||||
store this information as a 0 (false) and 1 (true). \matlab{} knows a
|
||||
store this information as 0 (false) or 1 (true). \matlab{} knows a
|
||||
special data type (\codeterm{logical}) to store the result of a
|
||||
Boolean expression. Every variable can be evaluated to true or false
|
||||
by converting it to the logical data type. When doing so \matlab{}
|
||||
|
Reference in New Issue
Block a user