0% found this document useful (0 votes)
944 views5 pages

Feedback - V. Octave Tutorial

The student received full scores on a 5 question Octave matrix operations quiz. The questions covered topics such as matrix multiplication, indexing, vectorization, and element-wise operations. The student demonstrated a strong understanding of how to perform common linear algebra operations in Octave.

Uploaded by

Sai Harika
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
944 views5 pages

Feedback - V. Octave Tutorial

The student received full scores on a 5 question Octave matrix operations quiz. The questions covered topics such as matrix multiplication, indexing, vectorization, and element-wise operations. The student demonstrated a strong understanding of how to perform common linear algebra operations in Octave.

Uploaded by

Sai Harika
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

3/27/2014 Quiz Feedback | Coursera

Feedback — V. Octave Tutorial Help

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

the transpose of A.)

Your Score Explanation


Answer

 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

Your Score Explanation


Answer

 0.25 The first element in Octave has index 1, so selecting columns 0


B = A(:, 0:2 through 2 is invalid.
);

 0.25 The first element in Octave has index 1, so this expression is


B = A(0:4, invalid.
0:2);

 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.

Your Score Explanation


Answer
https://class.coursera.org/ml-005/quiz/feedback?submission_id=666316 2/5
3/27/2014 Quiz Feedback | Coursera

 0.25 Octave will correctly perform the matrix-vector product equivalent to


v=A*x the for loop above.
;

 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);

 0.25 This is a well-defined product to compute a 10-vector, but it computes


v = x' * a different set of values.
A;

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.

Your Score Explanation


Answer

 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

z=v *w 7x7 matrix.


';

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.

One way to do so is the following code:

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

Which of the following correctly compute A, B, C , or D? Check all that apply.

Your Score Explanation


Answer

 0.25 The log function acts element-wise on matrix inputs.


A = log
(X);

 0.25 The .^ operator perfoms element-wise exponentiation.


B = X .^
2;

 0.25 Adding a single number applies element-wise to a matrix.


C = X+
1;

 0.25 The code X ^ 2 is equivalent to X * X which is only defined if X is a


B = X^ square matrix. To compute the square of each element, you need to
2; write X .^ 2.

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

You might also like