A430 of 56
0 XP
  1. Hook
  2. Predict
  3. Name it
  4. Check
  5. Explain

A4 · Lesson 30 · Two-dimensional arrays

Tables, grids and matrices

A data logger records eight channels at once: a table with a row per sample and a column per channel. A design sweep tries every combination of R and C: a grid. A circuit's node equations are a matrix. All three are two-dimensional arrays, and NumPy handles them with the same whole-array style as 1D ones, plus one new idea: the axis, which says whether an operation runs down the rows or across the columns.

Predict

X has shape (1000, 8): 1000 samples of 8 channels. What shape is X.mean(axis=0)?

Name it

Shape, indexing and slicing

X = np.array([[1, 2, 3],
              [4, 5, 6]])     # shape (2, 3): 2 rows, 3 columns
X[1, 2]                       # 6: row 1, column 2
X[0]                          # [1, 2, 3]: the first row
X[:, 1]                       # [2, 5]: the second column
X.T                           # the transpose, shape (3, 2)
X.reshape(3, 2)               # same numbers, new shape

Reducing along an axis

sum, mean, max, min, std and argmax all take axis=:

  • axis=0 works down the rows, leaving one value per column;
  • axis=1 works across the columns, leaving one value per row.

Grids for sweeps

np.meshgrid turns two 1D ranges into 2D grids, one value of each parameter at every point:

R = np.array([1e3, 2.2e3, 4.7e3, 10e3])
C = np.array([10e-9, 47e-9, 100e-9])
RR, CC = np.meshgrid(R, C)          # both shape (3, 4)
fc = 1 / (2 * np.pi * RR * CC)      # corner for every combination

Broadcasting with R[None, :] and C[:, None] does the same thing without making the grids.

To find the best combination, take the argmin of the whole grid and turn the flat index back into a row and column:

i = np.argmin(np.abs(fc - 1000))
row, col = np.unravel_index(i, fc.shape)
best = (R[col], C[row])

Matrices

A 2D array is also a matrix. A @ x is matrix-vector multiplication, and np.linalg.solve(A, b) solves A𝐱=𝐛. Lesson 37 and A7 use these for circuit equations.

Worked example

A logger recorded 4 channels at 5 samples. Find each channel's mean and the sample where channel 2 peaked.

Show the solution
  1. Average down the columns. axis=0 gives each channel's mean.
  2. Find channel 2's peak. Column 2 is X[:, 2].
X = np.array([[3.30, 5.01, 1.20, 0.00],
              [3.31, 4.99, 1.80, 0.10],
              [3.29, 5.02, 2.40, 0.05],
              [3.30, 5.00, 2.10, 0.00],
              [3.30, 4.98, 1.50, 0.08]])
print(X.mean(axis=0))          # [3.3, 5.0, 1.8, 0.046]
print(np.argmax(X[:, 2]))      # 2
  1. Read the peak. Channel 2's peak, 2.40, is at sample 2.

Answer the channel means are 3.3, 5.0, 1.8 and 0.046, and channel 2 peaks at sample 2.

Worked example

From the E6 resistors 1 kΩ to 6.8 kΩ and capacitors 10 nF to 68 nF, which pair gives a corner closest to 5 kHz?

Show the solution
  1. Find the time constant needed. RC=12π×5kHz=31.8μs
  2. Compute every pair at once. All 36 combinations are computed at once, and unravel_index turns the best one's position back into a choice of parts.
E6 = np.array([1.0, 1.5, 2.2, 3.3, 4.7, 6.8])
R, C = E6 * 1e3, E6 * 10e-9
fc = 1 / (2 * np.pi * R[None, :] * C[:, None])    # rows: C, columns: R
row, col = np.unravel_index(np.argmin(np.abs(fc - 5000)), fc.shape)
print(R[col], C[row], fc[row, col])
  1. Find the nearest product. The nearest product on offer is 33 µs, and four pairs give it: 1 kΩ with 33 nF, 1.5 kΩ with 22 nF, 2.2 kΩ with 15 nF and 3.3 kΩ with 10 nF.
  2. Its corner. All four give a corner of 4.82 kHz. argmin returns one of them; any is as good as another.

Answer any of the four pairs with RC=33 µs, such as 1 kΩ with 33 nF, gives 4.82 kHz, the closest to 5 kHz.

Check

The starter makes X, 200 samples of 5 channels. Set means to each channel's mean, rms to each channel's RMS (mean of squares), and loudest to the index of the channel with the largest RMS.

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Check

For every pair of E6 resistor (100 Ω to 680 kΩ) and capacitor (1 nF to 6.8 µF) in the starter, compute the corner frequency as a 2D array fc (rows for capacitors, columns for resistors). Set R_best, C_best to the pair whose corner is nearest target = 2000 Hz in ratio (smallest |log⁡(fc/target)|).

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Check

Set M to the n × n array (4 × 4) whose entry at row i, column j is 1/(i+j+1), using broadcasting (no loops). Then set diag to its diagonal and col_sums to the sum of each column.

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Check

The starter makes T, an n × n grid of temperatures (5 × 5). Using slices and no loops, set inner to the grid without its outer border, and avg4 to the average of each inner point's four neighbors (up, down, left and right), an array the same shape as inner.

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Check

An ADC sends m = 3 channels interleaved: channel 0's sample, then channel 1's, and so on, then channel 0 again. The starter's data holds 200 rounds of them. Set X to the samples as one row per round and one column per channel, and p2p to each channel's peak-to-peak value (its maximum minus its minimum).

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Check

X has shape (100, 8). What is the shape of X[:, 1:3]?

Explain why

For an array of shape (samples, channels), what's the difference between X.mean(axis=0) and X.mean(axis=1)?

Practice

Keep practicing

7 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.