astrochris
astrochris

Reputation: 1866

combining two arrays in numpy,

I use genfromtxt to read in an array from a text file and i need to split this array in half do a calculation on them and recombine them. However i am struggling with recombining the two arrays. here is my code:

X2WIN_IMAGE = np.genfromtxt('means.txt').T[1]
X2WINa = X2WIN_IMAGE[0:31]
z = np.mean(X2WINa)
X2WINa = X2WINa-z
X2WINb = X2WIN_IMAGE[31:63]
ww = np.mean(X2WINb)
X2WINb = X2WINb-ww
X2WIN = str(X2WINa)+str(X2WINb)
print X2WIN

How do i go about recombining X2WINa and X2WINb in one array? I just want one array with 62 components

Upvotes: 1

Views: 309

Answers (4)

Saullo G. P. Castro
Saullo G. P. Castro

Reputation: 59005

if you want to combine row-wise use np.vstack(), and if column-wise use np.hstack(). Example:

np.hstack( (np.arange(10), np.arange(10)) )
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9])


np.vstack( (np.arange(10), np.arange(10)) )
array([[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
       [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]])

Upvotes: 2

mgilson
mgilson

Reputation: 310237

And another one using numpy.r_:

X2WINc = np.r_[X2WINa,X2WINb]

e.g.:

>>> import numpy as np
>>> np.r_[np.arange(10),np.arange(10)]
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

There's also np.c_ to column stack:

>>> np.c_[np.arange(10),np.arange(10)]
array([[0, 0],
       [1, 1],
       [2, 2],
       [3, 3],
       [4, 4],
       [5, 5],
       [6, 6],
       [7, 7],
       [8, 8],
       [9, 9]])

Upvotes: 1

blazetopher
blazetopher

Reputation: 1050

 X2WINc = np.append(X2WINa, X2WINb)

Upvotes: 2

YXD
YXD

Reputation: 32521

combined_array = np.concatenate((X2WINa, X2Winb))

Upvotes: 1

Related Questions