Matrix indices warmup¶

First, About python range(a,b)¶

Run the following and make sure you understand the output

In [ ]:
a = range(10)
b = range(2,10)
c = range(2,10,3)
d = range(10,2,-1)
e = range(10,2,-3)
print(list(a))
print(list(b))
print(list(c))
print(list(d))
print(list(e))

Now for the matrix challenge¶

In [ ]:
import numpy as np

Write nested loops to create each of the following 5x5 matrices, starting from a zero matrix. Use 'c' and 'r' as the names of your index variables (this really helps your brain.) Use a counter, too! Your counter should always start at 1, and your code should always look like this, with no added lines

for c in ....
  for r in ......
    A[r][c] = ...
$$ A = \begin{bmatrix} 1 & 0 & 0 & 0 & 0\\ 2 & 6 & 0 & 0 & 0\\ 3 & 7 & 10 & 0 & 0\\ 4 & 8 & 11 & 13 & 0\\ 5 & 9 & 12 & 14 & 15 \end{bmatrix} $$
In [ ]:
A = np.zeros((5,5), np.int32)
# Solution 1 Here
print(A)
$$ B=\begin{bmatrix} 0 & 0 & 0 & 0 & 0\\ 1 & 0 & 0 & 0 & 0\\ 2 & 5 & 0 & 0 & 0\\ 3 & 6 & 8 & 0 & 0\\ 4 & 7 & 9 & 10 & 0 \end{bmatrix} $$
In [ ]:
B = np.zeros((5,5), np.int32)
# Solution 2 Here
print(B)
$$ C = \begin{bmatrix} 15 & 14 & 12 & 9 & 5\\ 0 & 13 & 11 & 8 & 4\\ 0 & 0 & 10 & 7 & 3\\ 0 & 0 & 0 & 6 & 2\\ 0 & 0 & 0 & 0 & 1 \end{bmatrix} $$
In [ ]:
C = np.zeros((5,5), np.int32)
# Solution 3 Here
print(C)
$$ D=\begin{bmatrix} 0 & 10 & 9 & 7 & 4\\ 0 & 0 & 8 & 6 & 3\\ 0 & 0 & 0 & 5 & 2\\ 0 & 0 & 0 & 0 & 1\\ 0 & 0 & 0 & 0 & 0 \end{bmatrix} $$
In [ ]:
D = np.zeros((5,5), np.int32)
# Solution 4 Here
print(D)