I am given a quadratic matrix and have to do as follows:
For each entry (i,j) in the matrix
If i = j:
set y[i,j] = x[i,j].
Else:
set y[i,j] = x[i,j] + x[j,i]
I have made the following script:
def symmetrize(x):
## The symmetrized matrix that is returned
y = np.zeros(np.shape(x))
## For loop for each element (i,j) in the matrix
for i in range (np.size(x)):
for j in range (np.size(x)):
if i == j:
y[i,j] = x[i,j]
else:
y[i,j] = x[i,j] + x[j,i]
return y
I get this error message whenever I want to run the code with the following matrix:
np.array([[1.2, 2.3, 3.4],[4.5, 5.6, 6.7], [7.8, 8.9, 10.0]])
Error message:
y[i,j] = x[i,j] + x[j,i]
IndexError: index 3 is out of bounds for axis 1 with size 3
Do someone know what the problem is?