problems with duplicating and slicing an array
John Hunter
jdhunter at ace.bsd.uchicago.edu
Thu Jan 20 22:51:11 EST 2005
>>>>> "Yun" == Yun Mao <yunmao at gmail.com> writes:
Yun> 2. Is there a way to do Matlab style slicing? e.g. if I have
Yun> i = array([0, 2]) x = array([1.1, 2.2, 3.3, 4.4]) I wish y =
Yun> x(i) would give me [1.1, 3.3] Now I'm using map, but it gets
Yun> a little annoying when there are two dimensions. Any ideas
Yun> would be deeply appreciated!
numarray supports matlab style indexing if you pass the ind as an
array or list of indices (but not a tuple, I found to my surprise).
As pointed out already, for Numeric you need to use the take function
>>> from numarray import array
>>> x = array([1,2,3,4,5,6,7,8,9])
>>> ind = [3,5,7]
>>> inda = array(ind)
>>> indt = tuple(ind)
>>> x[ind]
array([4, 6, 8])
>>> x[inda]
array([4, 6, 8])
>>> x[indt]
Traceback (most recent call last):
File "<stdin>", line 1, in ?
IndexError: too many indices.
I'm sure the tuple "surprise" is a documented feature.
JDH
More information about the Python-list
mailing list