hi I have 3 array and I wanna merge them element by element.for example
a=array([3.1, 3.3])
b=array([4.1, 4.3])
c=array([6.1, 1.3])
how I merge add them to get this
array([3.1, 4.1,6.1],[3.3,4.3,1.3])
hi I have 3 array and I wanna merge them element by element.for example
a=array([3.1, 3.3])
b=array([4.1, 4.3])
c=array([6.1, 1.3])
how I merge add them to get this
array([3.1, 4.1,6.1],[3.3,4.3,1.3])
You could first stack the arrays on top of each other
a=np.array([3.1, 3.3])
b=np.array([4.1, 4.3])
c=np.array([6.1, 1.3])
stacked = np.vstack([a, b, c])
and then transpose the result
result = stacked.T
which gives
>>> result
array([[3.1, 4.1, 6.1],
[3.3, 4.3, 1.3]])
In [374]: a=np.array([3.1, 3.3])
...: b=np.array([4.1, 4.3])
...: c=np.array([6.1, 1.3])
A common way to make a new array from others is with np.array
:
In [375]: np.array((a,b,c))
Out[375]:
array([[3.1, 3.3],
[4.1, 4.3],
[6.1, 1.3]])
Those are the numbers; just the layout is different, (3,2) as opposed to its transpose
.
Or use variations on concatenate/stack
. For example:
In [380]: np.column_stack((a,b,c))
Out[380]:
array([[3.1, 4.1, 6.1],
[3.3, 4.3, 1.3]])
In [381]: np.stack((a,b,c),axis=1)
Out[381]:
array([[3.1, 4.1, 6.1],
[3.3, 4.3, 1.3]])
Or even one call to transpose
(though under the cover this first does the [375] step):
In [382]: np.transpose((a,b,c))
Out[382]:
array([[3.1, 4.1, 6.1],
[3.3, 4.3, 1.3]])
These functions are all documented.