I spent about 2 hours trying to figure out what's going on, but for the life of me I can't figure out why switching the order of matrices when multiplying doesn't seem to work:
Using Python 2.7x:
import numpy as np
num_segments = 25
num_vintages = 24
# Create a 3d matrix 25 deep x 24r x 24c
mx_loans_new_loans = np.zeros((num_segments, num_vintages, num_vintages))
# Create the multiplication vector - same as the first dimension of 3d matrix
mult_vector = np.arange(10,35)
len(mult_vector)
The intent is to fill in the matrix on the diagonal with the mult_vector.
Here is the part I can't wrap my head around.
This version does not fulfill the intent:
for (i, x) in enumerate(mx_loans_new_loans):
np.fill_diagonal(x, 1)
x = x * mult_vector[i]
The results still spit out just the original matrix with 1's on the diagonal.
This version, however, does work. All I've done is reverse the matrices in the enumerate:
for (i, x) in enumerate(mult_vector):
np.fill_diagonal(mx_loans_new_loans[i], 1)
mx_loans_new_loans[i] = mx_loans_new_loans[i] * x
Sidenote: I've since realized a more optimized version fills the intent:
for (i, x) in enumerate(mx_loans_new_loans):
np.fill_diagonal(x, mult_vector[i])
But does anyone know why the first version doesn't work, but the second version does? What am I missing? Is this a broadcasting problem or something simpler?