numpy array merge

Robert Kern robert.kern at gmail.com
Tue Aug 11 06:33:03 CEST 2009


On 2009-08-10 17:38, Nathan wrote:
> Is there an easy way to merge two numpy arrays with different rank
> sizes (terminology?).

You will want to ask numpy questions on the numpy mailing list.

   http://www.scipy.org/Mailing_Lists

I believe that "shape" is the term you are looking for.

> I want to make a single array by concatenating
> two arrays along a given direction and filling the excess cells with a
> dummy variable.  numpy concatenate works well as long as the two
> arrays have the same dimension, but I want to do this on two array
> with a matching dimension size.  An example:
>
> import numpy as np
> a = np.array([[1,2,3],[4,5,6]])
> b = np.array([[1,2],[3,4],[5,6]])
> np.concatenate((a,a),axis=1)  #This works fine, but isn't what I need.
> Out:  array([[1, 2, 3, 1, 2, 3],
>         [4, 5, 6, 4, 5, 6]])
> np.concatenate((a,b),axis=1)  #This doesn't work, but this is what I
> need.
>
> I want to fill the third row of array "a" with a dummy variable (99999
> or NaN) and concatenate with "b" to make:
> [1,2,3,1,2]
> [4,5,6,4,5]
> [99999,99999,99999,5,6]
>
> This just seems like it would be relatively common.  So I thought I'd
> check if I'm missing something before I go write the logic.  Thanks!

I don't believe there is anything that does what you want out-of-box. You will 
probably want to determine the appropriate shape for the final array, use 
empty() to create it, use .fill(nan) (or whatever) to supply the default value, 
then assign the input arrays into the appropriate locations.

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
  that is made terrible by our own mad attempt to interpret it as though it had
  an underlying truth."
   -- Umberto Eco




More information about the Python-list mailing list