Re: fill in 3D array
- From: knielsen73@xxxxxxxxx
- Date: Wed, 23 Jul 2008 18:32:50 -0700 (PDT)
On Jul 23, 9:18 pm, Robert Kern <robert.k...@xxxxxxxxx> wrote:
knielse...@xxxxxxxxx wrote:
Hi,
I am a python newbie, trying to convert my IDL scripts to python. I am
kind of stuck at the moment. I am reading in a 1-D data file with 2000
data points. I need to put them in a 3-D array with size [10,10,20]. I
have defined the field array as arr = zeros((10,10,20)) but don't know
how to read the data into the array.
I assume that you are using numpy. Use numpy.fromfile() and the .reshape()
method. Assuming that your file is ASCII with numbers separated by whitespace:
import numpy
arr = numpy.fromfile(thefilename, sep=' ').reshape((10,10,20))
There is no need, in this case, to create an array before reading the data.
Also, I need to extract a slice of a 3-D array and tried a =
array_name(:,:,20) but that didn't work.
Python uses [] brackets for indexing, not ().
arr[:,:,20]
--
Robert Kern
"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
Thanks, working like a charm.
.
- Follow-Ups:
- Re: fill in 3D array
- From: Robert Kern
- Re: fill in 3D array
- References:
- fill in 3D array
- From: knielsen73
- Re: fill in 3D array
- From: Robert Kern
- fill in 3D array
- Prev by Date: Re: Request Help Debugging Program
- Next by Date: Broken examples
- Previous by thread: Re: fill in 3D array
- Next by thread: Re: fill in 3D array
- Index(es):
Relevant Pages
|