Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. (32%) Assume that A and B are MATLAB arrays with 10 rows and an equal number

ID: 2291305 • Letter: 1

Question

1. (32%) Assume that A and B are MATLAB arrays with 10 rows and an equal number of columns (the number of columns is not given). Important: Next, the expression "a single line of code" implies a single command or equality. In other words, the code:

X=A+1; X=X+B; is considered to be TWO lines of code, even though it can be written as one line.

(a) (3%) Write a single line of code which saves the first two rows of array A as a new array X.

(b) (4%) Write a single line of code which replaces rows 4 and 5 of array A with rows 2 and 6 of array B.

(c) (3%) Write a single line of code which divides all elements in array A by the corresponding elements in array B and saves the resulted array as X.

(d) (4%) Write a single line of code which adds the transpose of array A to array B and saves the result in array X. What should the number of columns of A and B be so that this operation does not result in an error?

(e) (4%) Write a single line of code which uses the MATLAB function "sum()" to find the sum of each row

(f) (3%) Lets call N the unknown number of columns of arrays A and B. What is the size of array X = [A B]; ?

(g) (3%) Again, N is the number of columns of arrays A and B. What is the size of array X = [A;B]; ?

(h) (3%) Assume that the arrays A and B have more than 5 columns. Write a single line of code that divides all elements in A by the B element located at row 3 and column 4.

(i) (5%) Write a single line of code using the MATLAB function "find()" which finds the location of all elements in A which are greater than 1. Then, write another line of code which replaces all these elements in A by the number 2.

Explanation / Answer

(i)

A = magic(10);
B = A';
X = A(1:2,:);

output:

(ii)

A([4 5],:) = B([2 6],:);

after modification A is

(iii)

X = A./B;

resultant X is

(iv)

X = A'+B; %A' denotes transpose

number of columns should be 10 as it is to be square