473,387 Members | 1,530 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

fill in 3D array

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.

Also, I need to extract a slice of a 3-D array and tried a =
array_name(:,:,20) but that didn't work.

Thanks,
Kim
Jul 24 '08 #1
3 2513
kn********@gmail.com 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

Jul 24 '08 #2
On Jul 23, 9:18 pm, Robert Kern <robert.k...@gmail.comwrote:
knielse...@gmail.com 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.
Jul 24 '08 #3
kn********@gmail.com wrote:
On Jul 23, 9:18 pm, Robert Kern <robert.k...@gmail.comwrote:
>knielse...@gmail.com 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]

Thanks, working like a charm.
Great. If you have more numpy questions, you will probably want to ask them on
the numpy mailing list instead of here.

http://www.scipy.org/Mailing_Lists

--
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

Jul 24 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Erwin Radermacher | last post by:
Hello dear PHP users, I have a form with text boxes and a dropdown menu with names of cities The adjacent textbox fills by the help of a Javascript function. My question is, how to realize the...
5
by: Fabio Papa | last post by:
Hi, I am fairly new to programming and and even newer to dotnet. I appoligise in advance if this is a dumb questions, and I would appreciate if you could answer it anyways. :) I am writing a...
5
by: Joe C | last post by:
I'm a hobbiest, and made the forray into c++ from non-c type languages about a year ago. I was "cleaning up" some code I wrote to make it more "c++ like" and have a few questions. I'm comfortable...
12
by: ma740988 | last post by:
I've allocated 4K memory and I'd like to use std::fill to fill each 1K with a different value. Note: I could easily use a vector/deque but I'm interested in a C style array. int main() { int...
19
by: Alex | last post by:
Hello list This question has probably already been asked, but let me ask again I have a mysql database to which I connect with my php scripts. The database contains articles. Name, Unit_Price...
14
by: romayankin | last post by:
Hello All, I'm writing cross-platform code so i'm bound to standards. Here is the code I have: ~~~~~~~~~~~~~~~~~~ double **mx = new double*; for(int i = 0; i < col - 1; i++) { mx = new...
2
by: cpisz | last post by:
I saw that using std::fill was the way to go for setting all elements of an array to some value in one foul swoop. However when I tryed it I am getting an error. Can I only use this for vectors...
0
by: dprjessie | last post by:
Hello, I am a Web programmer and I'm working on my first desktop application as a favor for a friend. I'm sure I have a stupid error here, but there is no error being thrown so I can't figure out...
1
by: Gennaro Prota | last post by:
Hi, I have the following function template template< typename T, std::size_t n > void secure_fill( volatile T ( &arr ), const T & value = T() ) { for( std::size_t i( 0 ); i < n; ++i ) {...
1
by: bytesFTW99 | last post by:
I have been struggling with this for some time can anyone help out? just trying to have 3 dropdown boxes that fill depending on what is selected, then in some cases click a button and have the second...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.