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

Dharhas Pothina Dharhas.Pothina@twdb.state.tx...
Wed May 21 13:42:30 CDT 2008


Thanks. I'm finding that scipy/numpy are very powerful (much more than matlab) but arrays/lists/tuples etc takes some getting used to.

- dharhas 

>>> "Anne Archibald" <peridot.faceted@gmail.com> 5/21/2008 1:36 PM >>>
2008/5/21 Dharhas Pothina <Dharhas.Pothina@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
_______________________________________________
SciPy-user mailing list
SciPy-user@scipy.org 
http://projects.scipy.org/mailman/listinfo/scipy-user



More information about the SciPy-user mailing list