I’m super new to programming and I’m trying to compute the dot product of all of the combinations of any tow rows in a N*3 matrix.
For example for N = 5 I have matrix
[0.64363829, 0.21027068, 0.7358777 ], [0.39138384, 0.49072791, 0.7784631 ], [0.22952251, 0.90537974, 0.35722115], [0.40108871, 0.88992243, 0.21717715], [0.06710475, 0.84022499, 0.53806962]
and I’d like to compute the dot products of all combinations of the rows like: row1*row2, row1*row3, row1*row4, row1*row5, row2*row3 … row4*row5.
I’m not sure how to approach this problem so I’ve tried a few things. So far I have
for i in range(N-1): for l in range(1, N): dotms = (np.dot(nmag[(i),:], nmag[(i+l),:])) print(dotms)
where nmag is the 5*3 matrix
the output only has 7 answers, but with 5 rows I’m looking for 10 different combinations
[0.9279489, 0.6009753, 0.6050964, 0.615819, 0.8122099, 0.7627538, 0.8574529]
Thank you in advance for your help!
Advertisement
Answer
Your loop indices don’t quite fit for your task:
import numpy as np nmag = np.array([[0.64363829, 0.21027068, 0.7358777 ], [0.39138384, 0.49072791, 0.7784631 ], [0.22952251, 0.90537974, 0.35722115], [0.40108871, 0.88992243, 0.21717715], [0.06710475, 0.84022499, 0.53806962]]) for i in range(N-1): # iterate over all rows but the last for j in range(i+1, N): # start j from i+1 dotms = np.dot(nmag[i, :], nmag[j, :]) print(dotms)