Could numpy.matlib.nanmax return a matrix?

Keith Goodman kwgoodman at
Wed Nov 15 11:55:38 CST 2006

On 11/12/06, Pierre GM <pgmdevlist at> wrote:
> On Sunday 12 November 2006 17:08, A. M. Archibald wrote:
> > On 12/11/06, Keith Goodman <kwgoodman at> wrote:
> > > Is anybody interested in making x.max() and nanmax() behave the same
> > > for matrices, except for the NaN part? That is, make
> > > numpy.matlib.nanmax return a matrix instead of an array.
> Or, you could use masked arrays... In the new implementation, you can add a
> mask to a subclassed array (such as matrix) to get a regular masked array. If
> you fill this masked array, you get an array of the same subclass.
> >>> import numpy as N
> >>> import numpy.matlib as M
> >>> import maskedarray as MA
> >>> x=M.rand(3,3)
> >>> assert isinstance(x.max(0), M.matrix)
> >>> assert isinstance(N.max(x,0), M.matrix)
> >>> assert  isinstance(MA.max(x,0).filled(0), M.matrix)
> >>> assert isinstance(MA.max(x,0)._data, M.matrix)
> >>> x[-1,-1] = N.nan
> >>> tmp = MA.max(MA.array(x,mask=N.isnan(x)), 0)
> >>> assert (tmp == N.nanmax(x,0)).all()
> >>> assert isinstance(tmp.filled(0), M.matrix)

I didn't know you could use masked arrays with matrices. I guess I
took the name literally.

I think an easier way to use masked arrays would be to introduce a new
thing called mis.

I could make a regular matrix

x  = M.rand(3,3)

and assign a missing value

x[0,0] = M.mis

x would then behave as a missing array matrix.

I could also do

x[M.isnan(x)] = M.mis


x[mask] = M.mis

To get the mask from x:




I think that would make missing arrays accessible to everyone.

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