3

Say I have two vectors:

A=linspace(-2,0,6)
B=linspace(0,2,6)

and I want to create a 2-dimensional matrix (of size 2 by 36) that matches all values in A to all values in B, so it would be something like:

[[-2 , 0]
[-2 , 0.4]
[-2 , 0.8]
[-2 , 1.2]
[-2 , 1.6]
[-2 , 2.0]
[-1.6 , 0]
[-1.6 , 0.4]
[-1.6 , 0.8]
....
....
[0 , 2.0]]

I guessing I would need some kind of for loop, but I'm not entirely sure on how to do this...

M4rtini
  • 13,186
  • 4
  • 35
  • 42
userk
  • 901
  • 5
  • 11
  • 19

4 Answers4

1

itertools.product is the tool that does exactly this

import itertools
result = array(list(itertools.product(A, B)))
6502
  • 112,025
  • 15
  • 165
  • 265
  • I just tried this, but get the error: NameError: name 'itertools' is not defined. Do I need to import the function from somewhere? Thanks – userk Feb 24 '14 at 20:00
  • Thanks, but this creates a list, is there a way of having a matrix output instead? – userk Feb 24 '14 at 20:03
  • @userk: just apply the numpy `array` constructor to the list to get the matrix – 6502 Feb 24 '14 at 20:06
1

You could use nested list comprehensions to do this. For example:

[[a, b] for a in A for b in B]
Peter
  • 12,541
  • 3
  • 34
  • 39
1
>>> a = np.linspace(-2, 0, 6)
>>> b = np.linspace(0, 2, 6)
>>> 
>>> out = np.empty((len(a), len(b), 2))
>>> out[..., 0] = a[:, None]
>>> out[..., 1] = b[None, :]
>>> out = out.reshape(-1, 2)
>>> out
array([[-2. ,  0. ],
       [-2. ,  0.4],
       [-2. ,  0.8],
       [-2. ,  1.2],
       [-2. ,  1.6],
       [-2. ,  2. ],
       [-1.6,  0. ],
       [-1.6,  0.4],
       [-1.6,  0.8],
       [-1.6,  1.2],
       [-1.6,  1.6],
       [-1.6,  2. ],
       [-1.2,  0. ],
       [-1.2,  0.4],
       [-1.2,  0.8],
       [-1.2,  1.2],
       [-1.2,  1.6],
       [-1.2,  2. ],
       [-0.8,  0. ],
       [-0.8,  0.4],
       [-0.8,  0.8],
       [-0.8,  1.2],
       [-0.8,  1.6],
       [-0.8,  2. ],
       [-0.4,  0. ],
       [-0.4,  0.4],
       [-0.4,  0.8],
       [-0.4,  1.2],
       [-0.4,  1.6],
       [-0.4,  2. ],
       [ 0. ,  0. ],
       [ 0. ,  0.4],
       [ 0. ,  0.8],
       [ 0. ,  1.2],
       [ 0. ,  1.6],
       [ 0. ,  2. ]])
Jaime
  • 65,696
  • 17
  • 124
  • 159
0

If you are using a pandas.DataFrame this is achieved by performing a join operation between the two arrays, where each has been given a "key" that is constant for all of the entries.

You example would work like this:

In [271]: A=linspace(-2,0,6)

In [272]: B=linspace(0,2,6)

In [273]: A
Out[273]: array([-2. , -1.6, -1.2, -0.8, -0.4,  0. ])

In [274]: A = pandas.DataFrame({'A':linspace(-2,0,6)})

In [275]: B = pandas.DataFrame({'B':linspace(0,2,6)})

In [276]: A['key'] = 1

In [277]: B['key'] = 1

In [278]: pandas.merge(A, B, on='key')
Out[278]: 
      A  key    B
0  -2.0    1  0.0
1  -2.0    1  0.4
2  -2.0    1  0.8
3  -2.0    1  1.2
4  -2.0    1  1.6
5  -2.0    1  2.0
6  -1.6    1  0.0
7  -1.6    1  0.4
8  -1.6    1  0.8
9  -1.6    1  1.2
10 -1.6    1  1.6
11 -1.6    1  2.0
12 -1.2    1  0.0
13 -1.2    1  0.4
14 -1.2    1  0.8
15 -1.2    1  1.2
16 -1.2    1  1.6
17 -1.2    1  2.0
18 -0.8    1  0.0
19 -0.8    1  0.4
20 -0.8    1  0.8
21 -0.8    1  1.2
22 -0.8    1  1.6
23 -0.8    1  2.0
24 -0.4    1  0.0
25 -0.4    1  0.4
26 -0.4    1  0.8
27 -0.4    1  1.2
28 -0.4    1  1.6
29 -0.4    1  2.0
30  0.0    1  0.0
31  0.0    1  0.4
32  0.0    1  0.8
33  0.0    1  1.2
34  0.0    1  1.6
35  0.0    1  2.0

In [279]: pandas.merge(A, B, on='key')[['A','B']].values
Out[279]: 
array([[-2. ,  0. ],
       [-2. ,  0.4],
       [-2. ,  0.8],
       [-2. ,  1.2],
       [-2. ,  1.6],
       [-2. ,  2. ],
       [-1.6,  0. ],
       [-1.6,  0.4],
       [-1.6,  0.8],
       [-1.6,  1.2],
       [-1.6,  1.6],
       [-1.6,  2. ],
       [-1.2,  0. ],
       [-1.2,  0.4],
       [-1.2,  0.8],
       [-1.2,  1.2],
       [-1.2,  1.6],
       [-1.2,  2. ],
       [-0.8,  0. ],
       [-0.8,  0.4],
       [-0.8,  0.8],
       [-0.8,  1.2],
       [-0.8,  1.6],
       [-0.8,  2. ],
       [-0.4,  0. ],
       [-0.4,  0.4],
       [-0.4,  0.8],
       [-0.4,  1.2],
       [-0.4,  1.6],
       [-0.4,  2. ],
       [ 0. ,  0. ],
       [ 0. ,  0.4],
       [ 0. ,  0.8],
       [ 0. ,  1.2],
       [ 0. ,  1.6],
       [ 0. ,  2. ]])
ely
  • 74,674
  • 34
  • 147
  • 228