Slicing matrix
Robert Kern
robert.kern at gmail.com
Mon Apr 17 22:28:19 EDT 2006
Anthony Liu wrote:
> I am using numarray.
I will be using numpy for this post, and if you are new to numarray, then you
should probably skip it and use numpy instead. All new development is going
towards numpy.
http://numeric.scipy.org/
> Suppose I have
>
>>>>p = array(range(25), shape=(5,5))
>>>>p
>
> array([[ 0, 1, 2, 3, 4],
> [ 5, 6, 7, 8, 9],
> [10, 11, 12, 13, 14],
> [15, 16, 17, 18, 19],
> [20, 21, 22, 23, 24]])
In [4]: from numpy import *
In [5]: p = arange(25).reshape((5,5))
In [6]: p
Out[6]:
array([[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14],
[15, 16, 17, 18, 19],
[20, 21, 22, 23, 24]])
> How do I easily slice out [0,1,2]
In [7]: p[0, :3]
Out[7]: array([0, 1, 2])
> or [1,2,3]
In [9]: p[0, 1:4]
Out[9]: array([1, 2, 3])
> or [2,7,12]
In [10]: p[:3, 2]
Out[10]: array([ 2, 7, 12])
> or [7,12,17] and put it in a list?
In [11]: p[1:4, 2]
Out[11]: array([ 7, 12, 17])
--
Robert Kern
robert.kern at gmail.com
"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
More information about the Python-list
mailing list