Put pairwise differences of matrix rows in 3-d array
问题 I have a matrix Y of shape (n, d). I already calculated the pairwise row-differences in the following way: I, J = np.triu_indices(Y.shape[0], 0) rowDiffs = (Y[I, :] - Y[J, :]) No i want to create a 3d-array, containing the differences of the rows i and j of Y at position (i,j, :). How would you do it? The aim of it is to replace this inefficient loop: for i in range(Y.shape[0]): for j in range(Y.shape[0]): C[i,:] = C[i,:] + W[i, j] * (Y[i, :]-Y[j, :]) 回答1: I have found some success with this: