[SciPy-user] Generating a 4D array from a set of 3D arrays

Anne Archibald peridot.faceted at gmail.com
Wed May 21 14:36:59 EDT 2008


2008/5/21 Dharhas Pothina <Dharhas.Pothina at twdb.state.tx.us>:
>
> Thanks Anne, I don't know the final size so appending to a list is probably best. The array(big_list) seems to be working but for my future reference, what does the following do?
>
> big.append(read_3D_chunk()[np.newaxis,...])

Well, concatenate concatenates arrays along an already-existing axis,
so the arrays need to be four-dimensional when I concatenate them. So
I turn my three-dimensional arrays, of shape (a,b,c), into
four-dimensional arrays of shape (1,a,b,c); I later concatenate along
axis 0. The way to add this new length-1 axis is by using np.newaxis.
For example, to turn an array A of shape (2,3) into an array of shape
(2,1,3):

A = A[:,np.newaxis,:]

The "..." just means "as many : as needed"; very useful when writing
generic functions, but here I was just too lazy to write
[np.newaxis,:,:,:].

Anne



More information about the SciPy-User mailing list