suggestions for slices of matrices

Travis Oliphant oliphant.travis at
Tue Oct 10 01:52:22 CDT 2006

JJ wrote:
> Hello.
> I haven't been following development too closely
> lately, but I did just download and reinstall the
> current svn version.  For what its worth, I would like
> to again suggest two changes:
Suggestions are nice.  Working code is better.   Many ideas are just too 
difficult to implement (and still work with the system as it exists) and 
so never get done.  I'm not saying these ideas fit into this category, 
but generally if a suggestion is not taken it's very likely seen in that 
> --  If M is a nxm matrix and P and Z are nx1 (or 1xn)
> matrices, then it would be nice if we could write
> M[P==Z,:] to obtain all columns and only those rows
> where P==Z.
This works already if p and z are 1-d arrays.   That seems to be the 
only issue.  you want this to work with p and z being 2-d arrays (i.e. 
matrices).  The problem is there is already a defined behavior for this 
case that would have to be ignored (i.e. special-cased to get what you 
want).  This could be done within the special matrix sub-class of 
course, but I'm not sure it is wise.  Too many special cases make life 
difficult down the road.

It is better to just un-think the ambiguity between 1-d and 2-d arrays 
that was inspired by Matlab and recognize a 1-d situation when you have 
it.   But, that's just my opinion.  I'm not dead-set against 
special-casing in the matrix object if enough matrix-oriented people are 
in favor of it.  But, it would be a feature for a later NumPy (not 1.0).

>   Likewise, for 1xm (or mx1) matrices U and
> V, it would be nice to be able to use M[P==Z,U==V]. 
Same issue as before + cross-product versus element-by-element. 
> Also, it would be nice to use M[P==Z,U==2], for
> example, to obtain selected rows where matrix U is
> equal to a constant.
Again.  Form the cross-product using ix_().

> --  It would be nice to slice a matrix by using
> M[[1,2,3],[3,5,7]], for example.
You can get the cross-product using M[ix_([1,2,3],[3,5,7])].  This was a 
design choice and I think a good one. It's been discussed before.

> I believe this would help make indexing more user
> friendly.  In my humble opinion, I think indexing is a
> weak spot in numpy. 
I'm sorry you see it that way.  I think indexing is a strength of 
numpy.  It's a little different then what you are used to with Matlab, 
perhaps, but it is much more general-purpose and capable (there is one 
weak-spot in that a certain boolean indexing operations uses more memory 
than it needs to, but that is a separate issue...).  The Matlab behavior 
can always be created in a sub-class.

Best regards,


Take Surveys. Earn Cash. Influence the Future of IT
Join's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys -- and earn cash

More information about the Numpy-discussion mailing list