matplotlib.mlab.find() not quite like Matlab's

Hi,

In Matlab, the command

  [i,j] = find(mat >= 3)

causes i and j to hold the indices where the condition holds. (If
there is only one output argument, it will hold indices to the
flattened version of the condition.) Matplotlib's mlab.find() seems to
work for one-dimensional arrays only.

Here's what I'm using to emulate Matlab's find; it only seems to work
with Numeric, not numarray, and I have no idea whether this is an
efficient way to achieve the goal. I was going to suggest that the
utility be included in matplotlib, but perhaps it should then be
generalized to work with numarray as well. I wonder if anyone has any
ideas on how to do this? I'm a newcomer to matplotlib (and Numeric,
etc.), so please do point out if there is a simpler way to achieve the
effect of Matlab's find.

def find(condition):
    """
    Return the indices where condition is true.
    For arrays of N>=2 dimensions, returns a tuple T of N arrays
    such that the condition is true at indices (T[0][i],...,T[N-1][i]).
    """

    sh = condition.shape
    if len(sh) == 1:
        return nonzero(condition)
    idx = indices(sh)
    cond = ravel(condition)
    return tuple([compress(cond, ravel(idx[i]))
                  for i in range(len(sh))])

···

--
Jouni K Seppänen
http://www.iki.fi/jks

Oops - actually it seems that after

from matplotlib.numerix import ravel, indices, compress

the function works whether numerix is using numarray or Numeric.

Making a special case of single-dimensional inputs is not very
elegant, but doing otherwise would break compatibility with the
existing mlab.find; and I guess there is no way in Python of emulating
Matlab's detection of the number of output arguments.

···

On Sun, 14 Nov 2004 18:08:59 +0200, Jouni K Seppänen <jouni.seppanen@...287...> wrote:

Here's what I'm using to emulate Matlab's find; it only seems to work
with Numeric, not numarray

--
Jouni K Seppänen

Hi Jouni,

Jouni K Seppänen wrote:

···

On Sun, 14 Nov 2004 18:08:59 +0200, Jouni K Seppänen ><jouni.seppanen@...287...> wrote:

Here's what I'm using to emulate Matlab's find; it only seems to work
with Numeric, not numarray
   
Oops - actually it seems that after

from matplotlib.numerix import ravel, indices, compress

the function works whether numerix is using numarray or Numeric.

Making a special case of single-dimensional inputs is not very
elegant, but doing otherwise would break compatibility with the
existing mlab.find; and I guess there is no way in Python of emulating
Matlab's detection of the number of output arguments.

I looked at this a while back, and came to the same conclusion. Thats an interesting question, can Python determine the number of output arguments? I dont know of a convenient function call, but it seems like that information must exist somewhere. I'll ask at c.l.p.

Darren

Hello,

I think I've found a bug in mathtext - sorry if this appeared
before, I couldn't find it in the list archives.

When I try:

xlabel(r"$-(\Omega_a^' + \Omega_a)$")

I don't get what I expected. The label shows -\Omega_a and stops
there. Further tests showed that, whenever I try a single quote
in a mathtext string, the text is not rendered from the quote on.

This might not be a bug, I might be doing something wrong. If
this is the case, please tell me how to do it right.

Thanks in advance.

···

---
Jos� Alexandre Nalon
nalon@...166...

xlabel(r"\-\(\\Omega\_a^&#39; \+ \\Omega\_a\)")

I don't get what I expected. The label shows -\Omega_a and stops
there. Further tests showed that, whenever I try a single quote
in a mathtext string, the text is not rendered from the quote on.

This might not be a bug, I might be doing something wrong. If
this is the case, please tell me how to do it right.

try replacing ^' with ^\prime

Darren