[SciPy-User] noob question about slicing / extracting numbers from array...
Oleksandr Huziy
guziy.sasha@gmail....
Sun Jul 8 11:27:39 CDT 2012
Hi newsboost,
sorry, I thought you were asking why you are getting the exception : out of
bounds.
You have to reinitialize xyz at the beginning of each time step if you want
to iterate through the same bodies on each time step.
In order to get what you need you write as follows test = q[xyz, x].
please, see the attached script, which works as follows on my system:
>>>
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 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
------------------
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]
Body: 2 --- q [14 15 16] [ 1 ] =
q[xyz][x] = [35 36 37]
>>>
Cheers
--
Oleksandr (Sasha) Huziy
2012/7/8 newsboost <newsboost@gmail.com>
> Uh, sorry - this has exactly the same behaviour on my system as before...
> Why should it help to re-init xyz in your opinion in the x-loop?
>
> My problem is that I want to process all columns (only two here) one at a
> time and then I want to extract 3 numbers for each body. Now I only get 2
> numbers... Are you familiar with the Matlab notation:
>
> q(xyz, x) ???
>
> That is exactly what I want... 3 numbers from each x-column... That's why
> I tried: q[xyz][x]
> It seems like I'm getting two numbers from the first value (of the
> 3-vector xyz, so the last two numbers in xyz is not used) which corresponds
> to the row... But what I want is 3 numbers (those from xyz) at the current
> x-column...
>
> Sorry, I don't think your suggestion worked. Look forward to hear other
> suggestions. Thanks.
>
>
> On 07/08/2012 06:06 PM, newsboost wrote:
>
> *Oleksandr Huziy* guziy.sasha@gmail.... <scipy-user%40scipy.org?Subject=%5BSciPy-User%5D%20noob%20question%20about%20slicing%20/%20extracting%20numbers%0A%20from%20array...&In-Reply-To=4FF90B80.8080105%40gmail.com>
> *Sun Jul 8 09:29:56 CDT 2012*
> ------------------------------
>
> Before the loop over bodies reinitialize xyz:
>
>
> for x in range(0,timesteps):
> print
> print "Step: ", x
> print "------------------"
> xyz=scipy.array([0,1,2]) #<------ reinit
> 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
>
> 2012/7/8 newsboost <newsboost@gmail.com <http://mail.scipy.org/mailman/listinfo/scipy-user>>
>
> >* 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...*>**
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/scipy-user/attachments/20120708/e8298751/attachment-0001.html
-------------- next part --------------
A non-text attachment was scrubbed...
Name: test.py
Type: application/octet-stream
Size: 642 bytes
Desc: not available
Url : http://mail.scipy.org/pipermail/scipy-user/attachments/20120708/e8298751/attachment-0001.obj
More information about the SciPy-User
mailing list