[PYTHON MATRIX-SIG] take() in 1.0a2 (longish, sorry)
Carlos Fonseca
fonseca@gaivota.demon.co.uk
Sat, 17 Aug 1996 20:07:10 +0100 (BST)
Hi,
I don't know if the following is intentional or not:
>>> from Numeric import *
>>> a=array([[2,3,7,6,4,7],[6,9,6,4,3,2]])
>>> a
2 3 7 6 4 7
6 9 6 4 3 2
>>> sort(a,-1)
2 3 4 6 7 7
2 3 4 6 6 9
>>> argsort(a,-1)
0 1 4 3 2 5
5 4 3 0 2 1
>>> take(a,argsort(a,-1),-1)
2 3 4 6 7 7
7 4 6 2 7 3
6 9 3 4 6 2
2 3 4 6 6 9
>>>
I.e., when take is given a 2 dimensional indices array, it adds yet
another dimension to a. This should not happen if take(a,argsort(a,-1),-1)
is to be equivalent to sort(a,-1). Note that, in the example above,
indices is perfectly aligned with a.
In general, if
b=take(a,indices,axis)
then
b[i,...,j,...,k]=a[i,...,indices[i,...,j,...,k],...,k]
^
axis
with broadcasting of dimensions for all axes other than the "active" axis.
Since a and indices would normally be aligned at the last dimension,
at least the following should be true:
a
2 3 7 6 4 7
6 9 5 4 3 2
take(a,[1,0,1],-1) # select columns
3 2 3
9 6 9
take(a,[1,0,1],-2)
Arrays not aligned!
take(a,[[1],[0],[1]],-1)
Arrays not aligned!
take(a,[[1],[0],[1]],-2) # select rows
6 9 5 4 3 2
2 3 7 6 4 7
6 9 5 4 3 2
As for the default axes, oh well... :-)
Carlos
=================
MATRIX-SIG - SIG on Matrix Math for Python
send messages to: matrix-sig@python.org
administrivia to: matrix-sig-request@python.org
=================