Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create matrix with 2 arrays in numpy

I want to find a command in numpy for a column vector times a row vector equals to a matrix

[1,1,1,1 ] ^T * [ 2,3 ] = [[2,3],[2,3],[2,3],[2,3]]

like image 512
user3754245 Avatar asked Nov 15 '25 15:11

user3754245


1 Answers

First, let's define your 1-D numpy arrays:

In [5]: one = np.array([ 1,1,1,1 ]); two = np.array([ 2,3 ])

Now, lets multiply them:

In [6]: one[:, np.newaxis] * two[np.newaxis, :]
Out[6]: 
array([[2, 3],
       [2, 3],
       [2, 3],
       [2, 3]])

This used numpy's newaxis to add the appropriate axes to get a 4x2 output matrix.

like image 98
John1024 Avatar answered Nov 17 '25 08:11

John1024



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!