I have an array of A of shape (N, 3) and an array B of shape (M, 3), where M < N.
I want to create some kind of array shape C of shape (M, N, 3), where C[i, :, :] of shape (3 ,N) is the difference between all points in A and the ith point of B:
C[i, :, :] == A - B[i]
Orr more explicitly:
C[i, :, :] == A[:, :] - B[i, :]
What is the best way to do it easily with vectorized Numpy operations, without having to rely on loops?
>Solution :
You can broadcast:
C = A[None] - B[:, None]
Shapes:
# A[None].shape
(1, N, 3)
# B[:,None].shape
(M, 1, 3)
# C.shape
(M, N, 3)