[Numpy-discussion] dumb question about creating a complex array
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)
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,a.shape/2), dtype=N.complex)
>>> b.real = a[:,range(0,a.shape,2)]
>>> b.imag = a[:,range(1,a.shape,2)]
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.
Christopher Barker, Ph.D.
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
More information about the Numpy-discussion