Chapter 9: Exercises
Part 1
- Matrix determinant and product of eigenvalues:
在數學上,我們可以證明:一個方陣的行列式,會等於它的 eigenvalue 的乘積。試寫一個 MATLAB 程式,任意產生不同維度的 10 個方陣,來驗證上述恆等式。
- tr(A) 定義為方陣 A 的主對角線元素的和,在數學上,只要矩陣 A 和矩陣 B 的乘積是一個方陣,我們可以證明 tr(AB)=tr(BA)。試寫一個 MATLAB 程式,任意產生不同維度的 10 組 A、B,以驗證上述恆等式。
- 我們已經知道 sort 指令的用法是 [sortedVec, index] = sort(vec),請寫一個函數 invSort,是 sort 的反運算,產生的效果是 vec = invSort(sortedVec, index)。
提示:你可以使用 sort 指令來快速地達到此功能。
- 請設計一個函數 index2pos.m,其用法如下:
position = index2pos(index);
其中 index 是一個元素為正整數的向量,position 則是此函數的輸出矩陣,其列數為 index 元素的最大值,行數則是 index 的元素個數,而且當 index(i) = j 時,position 的第 i 行中,只有第 j 個元素為 1,其餘為零。例如,當 index 是 [1 3 5 2] 時,傳回的 position 是
$$
\left[
\begin{matrix}
1 & 0 & 0 & 0\\
0 & 0 & 0 & 1\\
0 & 1 & 0 & 0\\
0 & 0 & 0 & 0\\
0 & 0 & 1 & 0\\
\end{matrix}
\right]
$$
- 請設計一個函數 copyMatrix.m,其用法如下:
bigMatrix = copyMatrix(matrix, p, q);
其中 matrix 是一個輸入矩陣,p 和 q 是正整數,函數 copyMatrix 的作用是將 matrix 視為一個元素,並將之複製成 p x q 的大矩陣 bigMatrix,其列數為 p*size(matrix,1),行數為 q*size(matrix,2)。例如,當 a = [1 2; 3 4] 時,copyMatrix(a, 2, 5) 所產生的輸出矩陣是:
$$
\left[
\begin{matrix}
1 & 2 & 1 & 2 & 1 & 2 & 1 & 2 & 1 & 2\\
3 & 4 & 3 & 4 & 3 & 4 & 3 & 4 & 3 & 4\\
1 & 2 & 1 & 2 & 1 & 2 & 1 & 2 & 1 & 2\\
3 & 4 & 3 & 4 & 3 & 4 & 3 & 4 & 3 & 4\\
\end{matrix}
\right]
$$
- 設計一個函數 copyElement.m,其用法如下:
bigMatrix = copyElement(matrix, p, q);
其中 matrix 是一個輸入矩陣,p 和 q 是正整數,函數 copyElement 的作用是將 matrix 的每一個元素複製成 p x q 的矩陣後,再合成一個大矩陣 bigMatrix,其列數為 p*size(matrix,1),行數為 q*size(matrix,2)。例如,當 a = [1 2; 3 4] 時,copyElement(a, 2, 5) 所產生的輸出矩陣是:
$$
\left[
\begin{matrix}
1 & 1 & 1 & 1 & 1 & 2 & 2 & 2 & 2 & 2\\
1 & 1 & 1 & 1 & 1 & 2 & 2 & 2 & 2 & 2\\
3 & 3 & 3 & 3 & 3 & 4 & 4 & 4 & 4 & 4\\
3 & 3 & 3 & 3 & 3 & 4 & 4 & 4 & 4 & 4\\
\end{matrix}
\right]
$$
- 執行下列程式碼後,請問 size(X) 和 size(y) 各是多少?
x = rand(10,8,16);
y = permute(x, [2 3 1]);
- Give an expression that multiplies two vectors to obtain the matrix [1 2 3 4 5;1 2 3 4 5;1 2 3 4 5].
Ans: [1 1 1]'*[1 2 3 4 5]
Part 2
- Extract nonzero elements in a vector:
How do you write a single MATLAB statement to extract nonzero elements of a vector A and assign them to A2?
Ans:
A2=A(A~=0); or A2=A(find(A));
- Matrix indexing:
Matrix A is expressed as
$$
A=
\left[
\begin{matrix}
4 & 10 & 1 & 6 & 2\\
8 & 2 & 9 & 4 & 7\\
7 & 5 & 7 & 1 & 5\\
0 & 3 & 4 & 5 & 4\\
23 & 13 & 13 & 0 & 3\\
\end{matrix}
\right]
$$
Which are the correct ways to assign the last column to matrix B?
- B=A(1:5, 5);
- B=A(1:end, end);
- B=A(:, 5);
- B=A(:, end);
- B=A(21:25);
- B=A(21:end);
- More on matrix indexing:
Given a 2-dimensional matrix A, write a one-line MATLAB statement (as simple as possible) to achieve the following goals:
- Assign the first column of matrix A to variable B.
- Assign the last column of matrix A to variable B.
- Assign the second column from the end of matrix A to variable B.
- Assign the second row from the end of matrix A to variable B.
- Assign the maximum value of matrix A to variable B.
- About 'diag' command:
Suppose that A = [1 2 3; 4 5 6; 7 8 9].
- What is returned by diag(A)?
- What is returned by diag(diag(A))?
- Using 'diag' command:
Given a matrix A, you want to divided each column by its column sum, and assign the column-normalized version to variable B.
- Write a one-line MATLAB statement to achieve the goal, which involves the use of 'diag' command.
- Write a one-line MATLAB statement to achieve the goal, which involves the use of 'bsxfun' command.
- About 'hilb' command:
Plot log(det(hilb(n))) versus n when n is varied from 1 to 40. Which value of n will make det(hilb(n)) zero?
- About transpose:
Suppose that A = [1+i, 2; 3, 1+2i].
- What is A'?
- What is A.'?
- Lp-norm:
- Give the definition of Lp-norm for a vector $\mathbf{x}=[x_1, x_2, ..., x_n]$.
- Let $L_p(x)$ be the Lp-norm of a vector $x$. Prove that $L_1(\mathbf{x}) \geq L_2(\mathbf{x}) \geq L_\infty(\mathbf{x})$.
- Derive the explicit analytic form of $L_\infty(\mathbf{x})$.
- In a 2D space, plot the following sets of $\mathbf{x}=[x_1, x_2]$:
- $\{\mathbf{x}|L_1(\mathbf{x})=1\}$
- $\{\mathbf{x}|L_2(\mathbf{x})=1\}$
- $\{\mathbf{x}|L_\infty(\mathbf{x})=1\}$
- Sorting and its inverse:
For a given vector x, we can sort its element using the "sort" command:
[x2, index]=sort(x);
the sorted vector x2 is actually equal to x(index). Write a function "sortInv" that returns x from given x2 and index, that is:
x=sortInv(x2, index);
To test your function, try the following script and the value of z should be 1:
x=round(100*rand(1,100));
[x2, index]=sort(x);
x3=sortInv(x2, index);
z=isequal(x, x3)
(Hint: You can use "sort" command to create the function, which can be as short as two major lines of statements in the function body.)
- Various integer data types:
How many bytes are used for each of the following data types? And what is the corresponding numerical range of the data type?
- uint8
- uint16
- uint32
- int8
- int16
- int32
- Other data types:
How many bytes are used for each of the following data types?
- single
- double
- char
- Operations on uint8:
Suppose that v=uint8([4, 9, 2]). Give the values of the following expressions in MATLAB.
- 2.5*v
- 2.4*v
- 50*v
- -50*v
- Operations on int8:
Suppose that v=int8([4, 9, 2]). Give the values of the following expressions in MATLAB.
- 2.5*v
- 2.4*v
- 50*v
- -50*v
- 請參考下列程式碼:
n = 10000;
vec = rand(n, 1);
ratio = sum(vec>0.7)/n;
當 n 的值越來越大時,ratio 的值會逼近於多少?為什麼?
MATLAB程式設計:入門篇