1

I have two matrices X,Y of size (m x d) and (n x d) respectively. Now i want to subtract the whole matrix Y from each element of the matrix X to get a third matrix Z of size (m x n x d). Using loops it would look this:

Z = [(Y-x) for x in X]

but i want to avoid loops and use numpy only.

K. Sama
  • 13
  • 1
  • 4

1 Answers1

3

If i understand correctly, here is a small demo:

In [81]: X = np.arange(6).reshape(2,3)

In [82]: Y = np.arange(12).reshape(4,3)

In [83]: X
Out[83]:
array([[0, 1, 2],
       [3, 4, 5]])

In [84]: Y
Out[84]:
array([[ 0,  1,  2],
       [ 3,  4,  5],
       [ 6,  7,  8],
       [ 9, 10, 11]])

In [85]: X.shape
Out[85]: (2, 3)

In [86]: Y.shape
Out[86]: (4, 3)

In [87]: Z = Y - X[:, None]

Result:

In [95]: Z
Out[95]:
array([[[ 0,  0,  0],
        [ 3,  3,  3],
        [ 6,  6,  6],
        [ 9,  9,  9]],

       [[-3, -3, -3],
        [ 0,  0,  0],
        [ 3,  3,  3],
        [ 6,  6,  6]]])

In [96]: Z.shape
Out[96]: (2, 4, 3)
MaxU - stand with Ukraine
  • 205,989
  • 36
  • 386
  • 419
  • I tried to use your method for large matrices, but it returns MemoryError. Example: `X = np.arange(540000).reshape(180000,3)` and `Y = np.arange(150000).reshape(50000,3)` consequently: `Z = Y - X[:, None]` returns `MemoryError` – Mubeen Shahid Oct 15 '19 at 22:24
  • 1
    @MubeenShahid, does your machine have `180000 * 50000 * 3 * X.dtype.itemsize` (which is approx. 100 GiB) of RAM to store the resulting matrix? ;) – MaxU - stand with Ukraine Oct 15 '19 at 23:13
  • Hi @MaxU , thanks for replying. The RAM size is 32GB. I am using loops at the moment but loop needs 1100 seconds (nearly 18 minutes) to process this data; hence I was looking for a vectorized method (which is the topic here); but it looks there is a memory-vs-speed issue. – Mubeen Shahid Oct 16 '19 at 06:46