## Part 1

1. Matrix determinant and product of eigenvalues: ¦b¼Æ¾Ç¤W¡A§Ú­Ì¥i¥HÃÒ©ú¡G¤@­Ó¤è°}ªº¦æ¦C¦¡¡A·|µ¥©ó¥¦ªº eigenvalue ªº­¼¿n¡C¸Õ¼g¤@­Ó MATLAB µ{¦¡¡A¥ô·N²£¥Í¤£¦Pºû«×ªº 10 ­Ó¤è°}¡A¨ÓÅçÃÒ¤W­z«íµ¥¦¡¡C
3. §Ú­Ì¤w¸gª¾¹D sort «ü¥Oªº¥Îªk¬O [sortedVec, index] = sort(vec)¡A½Ð¼g¤@­Ó¨ç¼Æ invSort¡A¬O sort ªº¤Ï¹Bºâ¡A²£¥Íªº®ÄªG¬O vec = invSort(sortedVec, index)¡C
´£¥Ü¡G§A¥i¥H¨Ï¥Î sort «ü¥O¨Ó§Ö³t¦a¹F¨ì¦¹¥\¯à¡C
4. ½Ð³]­p¤@­Ó¨ç¼Æ index2pos.m¡A¨ä¥Îªk¦p¤U¡G
position = index2pos(index);
¨ä¤¤ index ¬O¤@­Ó¤¸¯À¬°¥¿¾ã¼Æªº¦V¶q¡Aposition «h¬O¦¹¨ç¼Æªº¿é¥X¯x°}¡A¨ä¦C¼Æ¬° index ¤¸¯Àªº³Ì¤j­È¡A¦æ¼Æ«h¬O index ªº¤¸¯À­Ó¼Æ¡A¦Ó¥B·í index(i) = j ®É¡Aposition ªº²Ä i ¦æ¤¤¡A¥u¦³²Ä j ­Ó¤¸¯À¬° 1¡A¨ä¾l¬°¹s¡C¨Ò¦p¡A·í index ¬O [1 3 5 2] ®É¡A¶Ç¦^ªº position ¬O $$\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]$$
5. ½Ð³]­p¤@­Ó¨ç¼Æ copyMatrix.m¡A¨ä¥Îªk¦p¤U¡G
bigMatrix = copyMatrix(matrix, p, q);
¨ä¤¤ matrix ¬O¤@­Ó¿é¤J¯x°}¡Ap ©M q ¬O¥¿¾ã¼Æ¡A¨ç¼Æ copyMatrix ªº§@¥Î¬O±N matrix µø¬°¤@­Ó¤¸¯À¡A¨Ã±N¤§½Æ»s¦¨ p x q ªº¤j¯x°} bigMatrix¡A¨ä¦C¼Æ¬° p*size(matrix,1)¡A¦æ¼Æ¬° q*size(matrix,2)¡C¨Ò¦p¡A·í a = [1 2; 3 4] ®É¡AcopyMatrix(a, 2, 5) ©Ò²£¥Íªº¿é¥X¯x°}¬O¡G $$\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]$$
6. ³]­p¤@­Ó¨ç¼Æ copyElement.m¡A¨ä¥Îªk¦p¤U¡G
bigMatrix = copyElement(matrix, p, q);
¨ä¤¤ matrix ¬O¤@­Ó¿é¤J¯x°}¡Ap ©M q ¬O¥¿¾ã¼Æ¡A¨ç¼Æ copyElement ªº§@¥Î¬O±N matrix ªº¨C¤@­Ó¤¸¯À½Æ»s¦¨ p x q ªº¯x°}«á¡A¦A¦X¦¨¤@­Ó¤j¯x°} bigMatrix¡A¨ä¦C¼Æ¬° p*size(matrix,1)¡A¦æ¼Æ¬° q*size(matrix,2)¡C¨Ò¦p¡A·í a = [1 2; 3 4] ®É¡AcopyElement(a, 2, 5) ©Ò²£¥Íªº¿é¥X¯x°}¬O¡G $$\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]$$
7. °õ¦æ¤U¦Cµ{¦¡½X«á¡A½Ð°Ý size(X) ©M size(y) ¦U¬O¦h¤Ö¡H x = rand(10,8,16); y = permute(x, [2 3 1]);
8. 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

1. 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));
2. 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?
1. B=A(1:5, 5);
2. B=A(1:end, end);
3. B=A(:, 5);
4. B=A(:, end);
5. B=A(21:25);
6. B=A(21:end);
3. 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:
1. Assign the first column of matrix A to variable B.
2. Assign the last column of matrix A to variable B.
3. Assign the second column from the end of matrix A to variable B.
4. Assign the second row from the end of matrix A to variable B.
5. Assign the maximum value of matrix A to variable B.
4. About 'diag' command: Suppose that A = [1 2 3; 4 5 6; 7 8 9].
1. What is returned by diag(A)?
2. What is returned by diag(diag(A))?
5. 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.
1. Write a one-line MATLAB statement to achieve the goal, which involves the use of 'diag' command.
2. Write a one-line MATLAB statement to achieve the goal, which involves the use of 'bsxfun' command.
6. 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?
7. About transpose: Suppose that A = [1+i, 2; 3, 1+2i].
1. What is A'?
2. What is A.'?
8. Lp-norm:
1. Give the definition of Lp-norm for a vector $\mathbf{x}=[x_1, x_2, ..., x_n]$.
2. 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})$.
3. Derive the explicit analytic form of $L_\infty(\mathbf{x})$.
4. In a 2D space, plot the following sets of $\mathbf{x}=[x_1, x_2]$:
1. $\{\mathbf{x}|L_1(\mathbf{x})=1\}$
2. $\{\mathbf{x}|L_2(\mathbf{x})=1\}$
3. $\{\mathbf{x}|L_\infty(\mathbf{x})=1\}$
9. Sorting vectors: 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.)
10. 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?
1. uint8
2. uint16
3. uint32
4. int8
5. int16
6. int32
11. Other data types: How many bytes are used for each of the following data types?
1. single
2. double
3. char
12. Operations on uint8: Suppose that v=uint8([4, 9, 2]). Give the values of the following expressions in MATLAB.
1. 2.5*v
2. 2.4*v
3. 50*v
4. -50*v
13. Operations on int8: Suppose that v=int8([4, 9, 2]). Give the values of the following expressions in MATLAB.
1. 2.5*v
2. 2.4*v
3. 50*v
4. -50*v
14. ½Ð°Ñ¦Ò¤U¦Cµ{¦¡½X¡G n = 10000; vec = rand(n, 1); ratio = sum(vec>0.7)/n; ·í n ªº­È¶V¨Ó¶V¤j®É¡Aratio ªº­È·|¹Gªñ©ó¦h¤Ö¡H¬°¤°»ò¡H

MATLABµ{¦¡³]­p¡G¤Jªù½g