[Numpy-discussion] reshaping array question

Sebastian Berg sebastian at sipsolutions.net
Tue Nov 17 11:23:45 EST 2015


On Di, 2015-11-17 at 10:48 -0500, Neal Becker wrote:
> I have an array of shape
> (7, 24, 2, 1024)
> 
> I'd like an array of
> (7, 24, 2048)
> 
> such that the elements on the last dimension are interleaving the elements 
> from the 3rd dimension
> 

Which basically means you want to reshape with the earlier index varying
faster. This is fortran order (in the simplest case for two axes being
reshaped). So you can do:

arr.reshape((7, 24, -1), order="F")

otherwise, if order seems too confusing or dangerous. Just transpose the
two axes first:

arr_r = arr.transpose((0, 1, 3, 2))
arr_r = arr_r.reshape((7, 24, -1))

- Sebastian

> [0,0,0,0] -> [0,0,0]
> [0,0,1,0] -> [0,0,1]
> [0,0,0,1] -> [0,0,2]
> [0,0,1,1] -> [0,0,3]
> ...
> 
> What might be the simplest way to do this?
> 
> ------------
> A different question, suppose I just want to stack them
> 
> [0,0,0,0] -> [0,0,0]
> [0,0,0,1] -> [0,0,1]
> [0,0,0,2] -> [0,0,2]
> ...
> [0,0,1,0] -> [0,0,1024]
> [0,0,1,1] -> [0,0,1025]
> [0,0,1,2] -> [0,0,1026]
> ...
> 
> 
> 
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> https://mail.scipy.org/mailman/listinfo/numpy-discussion
> 

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: This is a digitally signed message part
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20151117/ff113c13/attachment.sig>


More information about the NumPy-Discussion mailing list