I have a symmetric matrix, and I want to loop over its elements. Currently, I'm using:
import torch
# sample symmetric matrix
W = torch.tensor([[0., 3., 0., 0., 0., 0., 0., 0., 0.],
[3., 0., 1., 0., 0., 0., 0., 0., 0.],
[0., 1., 0., 1., 1., 0., 0., 0., 0.],
[0., 0., 1., 0., 0., 0., 0., 0., 0.],
[0., 0., 1., 0., 0., 1., 0., 1., 0.],
[0., 0., 0., 0., 1., 0., 1., 0., 0.],
[0., 0., 0., 0., 0., 1., 0., 1., 0.],
[0., 0., 0., 0., 1., 0., 1., 0., 2.],
[0., 0., 0., 0., 0., 0., 0., 2., 0.]])
for ix, row in enumerate(W):
for iy, element in enumerate(row):
if iy <= ix:
continue
print(element)
I was wondering whether is this the most efficient way or if it can be written in a better way.
3., 0., 1.but the first column starts with3.0, 0., 0.\$\endgroup\$