You are on page 1of 5

MH2401

Algorithms & Computing III Lab 1: Solutions

AY 2013/14 S1

Tasks. 1. Try the following pairs of commands in MATLAB . Why do the pair of commands produce the same/dierent outputs? Pair (a) -2^4 (-2)^4 Pair (b) -2^4*3 Pair (c) 2/3*4 Pair (d) 2*3/4 Pair (e) 1+2:3+4 Pair (f) 1:3.^2 Pair (g) 1 < 2 < 3 Pair (h) 1 < 2 || 3 > 4
-2^(4*3) 2/(3*4) 2*(3/4) (1+2:3)+4 (1:3).^2 3 > 2 > 1 (1 < 2 || 3) > 4

Solution. Pair (a) The pair produces dierent outputs because ^ is of higher precedence than unary -; the rst command outputs the negative of the fourth power of two, while the second outputs the fourth power of negative two. Pair (b) The pair produces dierent outputs because ^ is of higher precedence than *; the rst command outputs the product of -2^4 and 3, while the second outputs the negative of the 4*3rd power of 2. Pair (c) The pair produces dierent outputs because / and * are of the same order of precedence; the rst command performs / before *, while the second performs * before /. Pair (d) The pair produces the same output even though / and * are of the same order of precedence, and the rst command performs * before /, while the second 3 3 performs / before *. This is because 2 and 2 4 are mathematically equal. 4 Pair (e) The pair produces dierent outputs because + is of higher precedence than :; the rst command adds both pairs of integers before forming a vector with :, while the second command adds the rst pair of integers, forms a vector with :, follows by adding every element of this vector to 4. Pair (f) The pair produces dierent outputs because .^ is of higher precedence than :; the rst command raises 3 to the power of 2 before forming a vector with :, while the second forms a vector with : before raising every element of this vector to the power of 2. Pair (g) The pair produces dierent outputs because operators of the same order of precedence are performed from left to right; the rst command compares 1 to 2 to get a logical 1 before comparing this result to 3, while the second compares 3 to 2 to get a logical 1 before comparing this result to 1.

Pair (h) The pair produces dierent outputs because relational operators are of higher precedence than logical operators; the rst command compares the two pairs of integers before taking their logical disjunction, while the second compares the rst pair of integers, takes the logical disjunction of the comparison with 2, follows by comparing this disjunction with 4. 2. Write down a MATLAB (a) 2 475/6 1 3 (b) 22 Solution. (a) 2*4^(-7*5/6)-1 (b) 2^(2^3) 3. Try the following commands with dierent real values for a. When a is an arbitrary real number, what outputs do the following MATLAB mands produce? Why? (a) 0 <= a <= 10 (b) a * (a > 0) (c) (a > 5) - (a < 5) command to evaluate each of the following expression.

com-

Solution. (a) This command always output 1 because the rst comparison gives either a 0 or 1, which are both less than 10. (b) This command outputs a when it is positive, and 0 otherwise. This is because the comparison gives 1 when a is positive, so that the product is a; and the comparison gives 0 otherwise, so that the product is still 0. (c) This command outputs 1 when a > 5, 0 when a = 5, and -1 when a < 5. This is because when a > 5, the two comparisons result in 1 - 0; when a = 5, the two comparisons result in 0 - 0; and when a < 5, the two comparisons result in 0 - 1. 4. Write down a MATLAB command to produce each of the following vector. In these vectors, n is a positive integer. (a) 2 4 8 2n +1) (b) 1 3 6 n(n2 Solution. (a) 2.^(1:n) (b) (1:n).*(2:n+1)/2 5. Write down a MATLAB command to produce each of the following matrix. In these matrices, m and n are positive integers.

0 1 2 (a) . . . m

1 1 1 . . . 1

2 1 1 . . . 1

n 1 1 . .. . . . 1

Solution. (a) [0 1:n; (1:m). ones(m,n)] (b) (1:m).*(1:n)

1 j n . . . . . . . . . (b) i i j i n . . . . . . . . . m m j m n

6. Try the following commands with dierent matrices with at least 5 rows and columns for M, and dierent integers between 1 and 5 for i and j. When M is an arbitrary matrix with at least 5 rows and columns, and i and j are arbitrary integers between 1 and 5, what outputs do the following MATLAB commands produce? (a) M(i, j) (e) size (M(:, 2:2:end),1) (b) M(i, :) (f) size (M(:, 2:2:end),2) (c) M(1:2, end-1:end) (g) M(end, :) == 0 (d) M(1:2:end, :) (h) M(end, :) = 0 Solution. (a) This command outputs the entry in the ith row and jth column of M. (b) This command outputs the ith row of M. (c) This command outputs the submatrix in the rst two rows and the last two columns of M. (d) This command outputs the submatrix formed by the odd rows of M. (e) This command outputs the entries in the number of rows of M. (f) This command outputs the entries in the number of even columns of M. (g) This command outputs a row vector of the same size as a row of M, which contains only 0 and 1, with an entry 1 when and only when the corresponding entry in the last row of M is 0. (h) This command outputs a matrix of the same size as M, which contains a last row of 0, and the same entries as M in all but the last row. 7. Try the following commands with dierent matrices with at least 5 rows and columns for M, and dierent integers between 1 and 5 for i. When M is an arbitrary matrix with at least 5 rows and columns, and i is an arbitrary integer between 1 and 5, what outputs do the following MATLAB commands produce? (a) M([1:2 end-1:end]) (b) M([i:size(M,1):end]) Solution. (a) This command outputs a row vector containing the rst two entries in the rst column of M, followed by the last two entries in the last column of M. (b) This command outputs the ith row of M.

8. Try the following commands with dierent matrices for M. When M is an arbitrary matrix, what outputs do the following MATLAB produce? (a) nd(M > 0) (b) M(M > 0)

commands

Solution. (a) This command outputs a column vector containing the linear indices of all positive entries of M. (b) This command outputs a column vector containing the positive entries of M.

Extra Questions. 1. Write one command to output the value of each of the following functions at x, where x is an arbitrary real number. x2 if x 1, (a) f (x) = 1 if x < 1. if sin(x) > 0.5, 0.5 (b) g (x) = sin(x) if 0.5 sin(x) 0.5, 0.5 if sin(x) < 0.5. Solution. (a) x^2*(x >= 1) + (x < 1) (b) 0.5*(sin(x)>0.5) + sin(x)*(-0.5<=sin(x) && sin(x)<=0.5) - 0.5*(sin(x)<
-0.5)

2. Use linear indexing to perform each of the following tasks with one command. (a) Extract the main diagonal of a square matrix M. I.e., form the vector [M(1,1), M(2,2), . . ., M(n,n)]. (b) Extract the anti-diagonal of a square matrix M. I.e., form the vector [M(n,1), M(n-1,2), . . ., M(1,n)]. Solution. (a) M([1:size(M,1)+1:end]) (b) M([size(M,1):size(M,1)-1:end-1]) 3. Write one command to change all negative entries of a matrix M to 2401. Solution.
M(M<0) = 2401

You might also like