[SciPy-User] noob question about slicing / extracting numbers from array...

newsboost newsboost@gmail....
Sat Jul 7 23:24:32 CDT 2012


Test program:
------------------------------------
#!/usr/bin/python

#import numpy as np
import scipy
import scipy.io
import sys
import numpy

# load data
timesteps = 2
bodies = 3
q = scipy.arange(0, bodies*7*timesteps).reshape(timesteps,bodies*7).T
print "type(q) = ", type(q)
print "q.shape =", q.shape
print "q = "
print q
print

LCS=scipy.zeros((bodies,3))
xyz=scipy.array([0,1,2])

for x in range(0,timesteps):
     print
     print "Step: ", x
     print "------------------"

     for b in range(0,bodies):
         # LCS[b] = q[xyz][x]
         print "Body: ", b, " --- q",xyz,"[",x,"] ="
         test = q[xyz][x]
         print "   q[xyz][x] = ", test
         xyz = xyz + 7 # go to next body
------------------------------------

Output:
===============
$ ./test.py
type(q) =  <type 'numpy.ndarray'>
q.shape = (21, 2)
q =
[[ 0 21]
  [ 1 22]
  [ 2 23]
  [ 3 24]
  [ 4 25]
  [ 5 26]
  [ 6 27]
  [ 7 28]
  [ 8 29]
  [ 9 30]
  [10 31]
  [11 32]
  [12 33]
  [13 34]
  [14 35]
  [15 36]
  [16 37]
  [17 38]
  [18 39]
  [19 40]
  [20 41]]


Step:  0
------------------
Body:  0  --- q [0 1 2] [ 0 ] =
    q[xyz][x] =  [ 0 21]
Body:  1  --- q [7 8 9] [ 0 ] =
    q[xyz][x] =  [ 7 28]
Body:  2  --- q [14 15 16] [ 0 ] =
    q[xyz][x] =  [14 35]

Step:  1
------------------
Body:  0  --- q [21 22 23] [ 1 ] =
Traceback (most recent call last):
   File "./test.py", line 30, in <module>
     test = q[xyz][x]
IndexError: index (21) out of range (0<=index<20) in dimension 0
===============

What I want:
************
Step:  0 (using data from the 1.column)
------------------
Body:  0  --- q [0 1 2] [ 0 ] =
    q[xyz][x] =  [ 0 1 2]
Body:  1  --- q [7 8 9] [ 0 ] =
    q[xyz][x] =  [ 7 8 9]
Body:  2  --- q [14 15 16] [ 0 ] =
    q[xyz][x] =  [14 15 16]

Step:  1 (etc. as above, but now using the 2. column)
------------------
Body:  0  --- q [0 1 2] [ 1 ] =
    q[xyz][x] =  [ 21 22 23]
Body:  1  --- q [7 8 9] [ 1 ] =
    q[xyz][x] =  [ 28 29 30]
etc.
************

Hope you understand... It's a noob question, I know... I just cannot 
make it right...

Thanks...



More information about the SciPy-User mailing list