Feedback - V. Octave Tutorial
Feedback - V. Octave Tutorial
You submitted this quiz on Thu 27 Mar 2014 6:38 PM IST. You got a score of 5.00
out of 5.00.
Question 1
Suppose I first execute the following Octave commands:
A = [1 2; 3 4; 5 6];
B = [1 2 3; 4 5 6];
Which of the following are then valid Octave commands? Check all that apply. (Hint: A'denotes
0.25 A' is 2x3 and B is 2x3, so A' does not have the same number of
C = A' * columns as B has rows, and the product is not well defined.
B;
0.25 A is 3x2 and B is 2x3, so A has the same number of columns as B has
C=A* rows, and the product is well defined.
B;
0.25 B is 2x3 and A is 3x2, so B has the same number of columns as A has
C=B* rows, and the product is well defined.
A;
0.25 B' is 3x2 and A is 3x2, so B' does not have the same number of
C = B' * columns as A has rows, and the product is not well defined.
A;
Total 1.00 /
1.00
Question 2
https://class.coursera.org/ml-005/quiz/feedback?submission_id=666316 1/5
⎡ ⎤
3/27/2014 Quiz Feedback | Coursera
16 2 3 13
⎡ ⎤
5 11 10 8
Let A = ⎢
⎢
⎥.
⎥
9 7 6 12
⎣ ⎦
4 14 15 1
16 2
⎡ ⎤
5 11
Which of the following indexing expressions gives B = ⎢
⎢
⎥?
⎥
Check all that apply.
9 7
⎣ ⎦
4 14
0.25 A(:, 1:2) selects every row and the first two columns of A, giving
B = A(:, 1:2 the desired B.
);
0.25 A(1:4, 1:2) selects the first four rows and first two columns of A,
B = A(1:4, giving the desired B.
1:2);
Total 1.00 /
1.00
Question 3
Let A be a 10x10 matrix and x be a 10-element vector. Your friend wants to compute the product
Ax and writes the following code:
v = zeros(10, 1);
for i = 1:10
for j = 1:10
v(i) = v(i) + A(i, j) * x(j);
end
end
How would you vectorize this code to run without any forloops? Check all that apply.
0.25 Octave does not implicitly multiply without * but instead will look for a
v = Ax; variable called "Ax".
0.25 The summation involved in the matrix-vector product occurs on its own
v = sum without needing to call the sum function explicitly.
(A * x);
Total 1.00 /
1.00
Question 4
Say you have two column vectors v and w, each with 7 elements (i.e., they have dimensions
7x1). Consider the following code:
z = 0;
for i = 1:7
z = z + v(i) * w(i);
end
Which of the following vectorizations correctly compute z? Check all that apply.
0.25 By taking the transpose of w, the product computes the sum of the
z = w' * element-wise product of w and v, just as the for-loop code does.
v;
0.25 By taking the transpose of v, the product computes the sum of the
z = v' * element-wise product of v and w, just as the for-loop code does.
w;
0.25 Recall that .* computes the element-wise product, not the matrix
z = v .* product, so the result here is also a 7x1 vector.
w;
0.25 v has dimension 7x1 and w' has dimension 1x7, so their product is a
https://class.coursera.org/ml-005/quiz/feedback?submission_id=666316 3/5
3/27/2014 Quiz Feedback | Coursera
Total 1.00 /
1.00
Question 5
In Octave, many functions work on single numbers, vectors, and matrices. For example, the sin
function when applied to a matrix will return a new matrix with the sin of each element. But you
have to be careful, as certain functions have different behavior. Suppose you have an 7x7 matrix
X. You want to compute the log of every element, the square of every element, add 1 to every
element, and divide every element by 4. You will store the results in four matrices, A, B, C , D.
for i = 1:7
for j = 1:7
A(i, j) = log (X(i, j));
B(i, j) = X(i, j) ^ 2;
C(i, j) = X(i, j) + 1;
D(i, j) = X(i, j) / 4;
end
end
https://class.coursera.org/ml-005/quiz/feedback?submission_id=666316 4/5
3/27/2014 Quiz Feedback | Coursera
Total 1.00 /
1.00
https://class.coursera.org/ml-005/quiz/feedback?submission_id=666316 5/5