[Numpy-discussion] dumb question about creating a complex array

Mathew Yeates myeates at jpl.nasa.gov
Thu Feb 22 18:52:06 EST 2007


Thanks for the replies. I think I have enough to work with.

Mathew


Christopher Barker wrote:
> Mathew Yeates wrote:
>   
>> given an array of floats, 2N columns and M rows, where the elements 
>> A[r,2*j] and A[r,2*j+1] form the real and imaginary parts of a complex 
>> number ....... What is the simplest way to create a complex array? It's 
>> a fairly large array so I want to keep copying to a minimum.
>>
>> (Actually, it's not a float array, its elements are byte sized, in case 
>> that matters)
>>     
>
> It does. If it was a float array, you may even be able to do it without 
> any copying at all. Anyway, this should work:
>
>  >>> a = N.array([[1,2,3,4],[2,3,4,5],[4,5,6,7]], dtype=N.byte)
>  >>> a
> array([[1, 2, 3, 4],
>         [2, 3, 4, 5],
>         [4, 5, 6, 7]], dtype=int8)
> # have I got that right?
>  >>> b = N.empty((a.shape[0],a.shape[1]/2), dtype=N.complex)
>  >>> b.real = a[:,range(0,a.shape[1],2)]
>  >>> b.imag = a[:,range(1,a.shape[1],2)]
>  >>> b
> array([[ 1.+2.j,  3.+4.j],
>         [ 2.+3.j,  4.+5.j],
>         [ 4.+5.j,  6.+7.j]])
>
>
> Is that what you wanted?
>
> By the way, I think there is a trick for doing the every other column 
> trick without using range(), but I can't find it at the moment.
>
> -Chris
>
>
>
>   


-- 







More information about the NumPy-Discussion mailing list