0

Given a 2-d numpy array X of dimension [m,m] and a 1-d array Y of length m, I wish to calculate a resulting 2-d matrix, P also of dimension [m,m], whose [i,j] element is obtained using the following operation:

P[i][j] = Y[i] * Y[j] * np.dot(X[i],X[j]) 

Is there a faster way rather than applying a nested for loop?

1 Answer 1

3

You can use:

P = Y*Y[:, None]*np.dot(X, X.T)

example:

>>> X = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
>>> Y = np.array([10, 11, 12])
>>> P = Y*Y[:, None]*np.dot(X, X.T)
>>> P
array([[ 1400,  3520,  6000],
       [ 3520,  9317, 16104],
       [ 6000, 16104, 27936]])
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.