Reputation: 69
I have a two numpy 1-D arrays: a and b.
a is of shape (10,) b is of shape (16,).
I want to make a have the shape (10,1) and b to have the shape (16,1) to matrix multiply them as so: np.matmul(a, b.T).
If successful, this should result in a (10,16) 2-D array. Does anyone know how to do this?
Upvotes: 0
Views: 1797
Reputation: 1
You can use reshape method.
import numpy as np
a = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]).reshape((10, 1))
b = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]).reshape((16, 1))
c = np.matmul(a, b.T)
print(c.shape)
Upvotes: 0
Reputation: 114548
You need to explicitly use reshape
. b.T
and b.transpose()
do not do anything meaningful for 1D arrays:
a = np.arange(10)
b = np.arange(16)
c = a.reshape(10, 1) @ b.reshape(1, 16)
c.shape
will now be (10, 16)
, as expected.
Upvotes: 0
Reputation: 210972
IIUC:
In [47]: a = np.arange(10)
In [48]: b = np.arange(16)
In [49]: a[:,None] * b
Out[49]:
array([[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15],
[ 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30],
[ 0, 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45],
[ 0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56, 60],
[ 0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75],
[ 0, 6, 12, 18, 24, 30, 36, 42, 48, 54, 60, 66, 72, 78, 84, 90],
[ 0, 7, 14, 21, 28, 35, 42, 49, 56, 63, 70, 77, 84, 91, 98, 105],
[ 0, 8, 16, 24, 32, 40, 48, 56, 64, 72, 80, 88, 96, 104, 112, 120],
[ 0, 9, 18, 27, 36, 45, 54, 63, 72, 81, 90, 99, 108, 117, 126, 135]])
Upvotes: 1