Skip to content
Advertisement

Compute the dot product of all combinations of two rows in a matrix

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

JavaScript

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

JavaScript

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:

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement