# [PYTHON MATRIX-SIG] A problem with slicing

**James Hugunin
**
jjh@mama-bear.lcs.mit.edu

*Fri, 15 Sep 95 11:29:09 -0400*

>* I've been playing with another technique for indexing a matrix, borrowed
*>* from matlab. I've implemented indexing matrices with a matrix of booleans
*>* (integers) that is the same size as the matrix being indexed (this only
*>* makes sense for a setvalue). This is trivially done using the mapping
*>* semantics, and combined with some matrix comparision operators I've found
*>* this quite useful.
*>*
*>* Indeed. But how does it work for higher-dimensional arrays?
*>*
*>* APL provides a similar functionality using an operator called
*>* "compress". Its boolean argument is always one-dimensional and it a
*>* works along a specified axis. If used on the right-hand side of an
*>* expression, it is simply filter(). In APL2 it may also be used on
*>* the left hand side of an assignment, as btw can all expressions
*>* that evaluate to a subset of elements of an array. This is an
*>* extremely powerful feature, but so complicated to implement that
*>* to my knowledge only IBM's mainframe version does it without
*>* restrictions.
*
My initial idea for higher-dimensional arrays was to use a matrix of
booleans that was the size of the entire higher-d array being indexed, this
fits naturally with the outputs from matrix comparision operators. ie.
a = [[1,2,3], [4,5,6]]
--> a.gt(2) == [[0,0,1], [1,1,1]]
a[a.gt(2)] = 99
--> a == [[1,2,99], [99,99,99]]
However, I agree that it should be possible to rationally apply this to a
subset of the matrix. Something like:
a = [[1,2,3], [4,5,6]]
--> sum(a).gt(7) == [0,1]
a[sum(a).gt(7), :] = 99
--> a == [[1,2,3], [99,99,99]]
This example suggests that it should be possible to mix these boolean
indices with range indices, and with single integer indices. While this all
sounds reasonable (and extremely powerful) to me, I have to confess that a
few of my feeping creaturism warning lights go off when I sit back and look
at it. I'd love to hear more impressions on this, pro and con.
-Jim
=================
MATRIX-SIG - SIG on Matrix Math for Python
send messages to: matrix-sig@python.org
administrivia to: matrix-sig-request@python.org
=================