[Numpy-discussion] dumb question about creating a complex array
Christopher Barker
Chris.Barker@noaa....
Thu Feb 22 17:28:22 CST 2007
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
--
Christopher Barker, Ph.D.
Oceanographer
Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception
Chris.Barker@noaa.gov
More information about the Numpy-discussion
mailing list