MORE INFO - Array assigment with Numeric.py
Morris, Brad [WDLN2:2W40:EXCH]
confused at americasm01.nt.com
Wed May 31 15:01:20 EDT 2000
Janko,
This is the routine that I used to load data from an ascii
file (this essentially highlighted the problem). The data is
separated by blanks and each line is terminated with an EOL
character. The file can be either a single line with
multiple entries separated by spaces, a single entry per
line with multiple lines, or a both. The data is regular, a
vector or a matrix, with no missing elements. There is a
final empty line in the file with an EOL only.The shapes
returned are (1,x), (x,1), or (x,y) respectively. Which in
second case is not the same as assigning the data to an
array directly (x,). It's not clear to me if this is a
function of data.append or Numeric.array(data). Does this
mean something is returning (x,NewAxis)?
Regards,
BJM
# Load matlab text file containing a single matrix/vector
import Numeric
import string
def ascii(filename):
data = []
f = open(filename, 'r')
# read entire file at once
d_string = f.readlines()
for i in range(len(d_string)):
data.append(map(string.atof, string.split(d_string[i])))
data = Numeric.array(data)
f.close
return data
Janko Hauser wrote:
>
> "Morris, Brad [WDLN2:2W40:EXCH]" <confused at americasm01.nt.com> writes:
>
> > QUESTION (1)
> > How can the elements in a vector be accessed in a consistent
> > manner regardless of shape (i.e. 1,2,or 3 above)?
> >
> If you know that you always have a vector you can ravle/flatten it
> with ravel(r) or r.flat if r is contigous.
>
> > QUESTION (2)
> > Are there some rules that define what is returned from a array operation
> > i.e. why sometimes (x,) and others (x,1)?
> >
> Which standard routines return (x,NewAxis)? No normally there is no
> standard rule, but it should not happen, that something like r2 is
> returned.
>
> Than I append a function which removes axis with length of one.
>
> HTH, __Janko
>
> def DelAxis(m):
> """
> Removes all axis with length one
> """
> sh = m.shape
> new_shape=[]
> for axis_length in sh:
> if axis_length > 1:
> new_shape.append(axis_length)
> return Numeric.reshape(m,new_shape)
>
> --
> Institut fuer Meereskunde phone: 49-431-597 3989
> Dept. Theoretical Oceanography fax : 49-431-565876
> Duesternbrooker Weg 20 email: jhauser at ifm.uni-kiel.de
> 24105 Kiel, Germany
More information about the Python-list
mailing list