Skip to content

Commit 5f3e81b

Browse files
Week 2 quiz 2 on Octave Tutorial.
1 parent a6437f0 commit 5f3e81b

File tree

1 file changed

+117
-0
lines changed

1 file changed

+117
-0
lines changed
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
# Week 2 | V. Octave Tutorial
2+
3+
## Question 1
4+
5+
6+
Suppose I first execute the following Octave commands:
7+
8+
A = [1 2; 3 4; 5 6];
9+
B = [1 2 3; 4 5 6];
10+
11+
Which of the following are then valid Octave commands? Check all that apply. (Hint: A' denotes the transpose of A.)
12+
13+
### Answer
14+
15+
* **C = B' + A;**
16+
17+
* C = A' * B;
18+
19+
* C = B + A;
20+
21+
* **C = A * B;**
22+
23+
24+
---
25+
26+
## Question 2
27+
![image](http://frog.isima.fr/cgi-bin/bruno/tex2png--10.cgi?A = \begin{bmatrix} 16 &2&3&13 \\\\5&11&10&8\\\\9&7&6&12\\\\4&14&15&1 \end{bmatrix})
28+
29+
Which of the following indexing expressions gives
30+
31+
![image](http://frog.isima.fr/cgi-bin/bruno/tex2png--10.cgi?B = \begin{bmatrix} 16 &2 \\\\5&11\\\\9&7\\\\4&14 \end{bmatrix})
32+
? Check all that apply.
33+
34+
### Answer
35+
36+
* B = A(:, 0:2);
37+
38+
* **B = A(:, 1:2);**
39+
40+
* **B = A(1:4, 1:2);**
41+
42+
* B = A(0:4, 0:2);
43+
44+
---
45+
46+
## Question 3
47+
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:
48+
49+
v = zeros(10, 1);
50+
for i = 1:10
51+
for j = 1:10
52+
v(i) = v(i) + A(i, j) * x(j);
53+
end
54+
end
55+
56+
57+
How would you vectorize this code to run without any for loops? Check all that apply.
58+
59+
### Answer
60+
61+
* **v = A * x;**
62+
63+
* v = sum (A * x);
64+
65+
* v = A .* x;
66+
67+
* v = Ax;
68+
69+
70+
---
71+
72+
## Question 4
73+
Say you have two column vectors v and w, each with 7 elements (i.e., they have dimensions 7x1). Consider the following code:
74+
75+
z = 0;
76+
for i = 1:7
77+
z = z + v(i) * w(i);
78+
end
79+
80+
Which of the following vectorizations correctly compute z? Check all that apply.
81+
82+
### Answer
83+
84+
* **z = v' * w;**
85+
86+
* z = v * w;
87+
88+
* **z = sum (v .* w);**
89+
90+
* z = v .* w;
91+
92+
93+
---
94+
95+
## Question 5
96+
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:
97+
98+
for i = 1:7
99+
for j = 1:7
100+
A(i, j) = log (X(i, j));
101+
B(i, j) = X(i, j) ^ 2;
102+
C(i, j) = X(i, j) + 1;
103+
D(i, j) = X(i, j) / 4;
104+
end
105+
end
106+
107+
Which of the following correctly compute A,B,C, or D? Check all that apply.
108+
109+
### Answer
110+
111+
* **B = X .^ 2;**
112+
113+
* **C = X + 1;**
114+
115+
* **D = X / 4;**
116+
117+
* B = X ^ 2;

0 commit comments

Comments
 (0)