[Numpy-discussion] conversion to complex

Warren Focke focke at slac.stanford.edu
Tue Mar 13 19:59:32 CST 2001


> >>> a=array([1,2,3,4], 'f')
> >>> b=zeros(len(a)/2, 'F')
> >>> b.real=a[0:4:2]
> >>> b.imag=a[1:4:2]
> >>> b
> array([ 1.+2.j,  3.+4.j],'F')

or, if you feel brave

> >>> c=fromstring(a.tostring(), 'F')
> >>> c
> array([ 1.+2.j,  3.+4.j],'F')

wbf

On Tue, 13 Mar 2001, Eric Hagemann wrote:

> I have an array of floats that is an interleaved stream of complex (real/imag) values that I want to promote to complex.  My current method uses the following
> 
> a=array([1,2,3,4])
> 
> b = a[0:4:2] + cmath.sqrt(-1)*a[1:4:2]
> 
> Is there a more elegant way ?  I am hoping to avoid explicit for loops etc
> 





More information about the Numpy-discussion mailing list