I have two matrices
fi = [[f1], Nij = [[N11 N12 .......N1n],
[f2], [N21 N22 .......N2n],
. ...
. ...
[fn]] [Nn1 Nn2 .......Nnn]]
I want to multiply:
f1 to each element of the 1st row,
f2 to each element of the 2nd row,
and so on.
I.e. I want Xij = fi*Nij
where fi is a column matrix and Xij & Nij is nxn matrix.
I tried using
import numpy as np
fi = np.linspace(1,5, num =5)
fi = np.asmatrix(fi)
def Xij(ai):
Nij = np.ones((5,5))
for i in range(len(fi)):
for j in range(len(fi)):
Xij[i,j] = ai[i] * Nij[i,j]
return Xij
Zij = Xij(fi)
It gives me this error TypeError: 'function' object does not support item assignment
Why? and how do I solve this?
Your problem is right here:
You named a variable
Xij
, but also a function. Furthermore, when you named the function, it overwrote the variable.Because of this, when you try to index the function and assign its items, an error is generated because you can't do that on the function. Below is an example:
Does that error look familiar? It is the same one you generated because I did exactly what you did.
To fix this problem, change the name of you function to something other than
Xij
. Doing so will makeXij
equal the matrice, which will support indexing and item assignment.