From: Perry G. <pe...@st...> - 2005-05-17 15:19:46
|
It's simpler in numarray: i,j =3D where(z<0.5) For numeric it's more work: ind =3D where(z.flat < 0.5) i =3D ind//z.shape[1] j =3D ind % z.shape[1] One could turn this into a function for Numeric (to handle all=20 dimensionalities, etc) On May 17, 2005, at 10:04 AM, Dimitri D'Or wrote: > Hello, > > =A0 > > Here under a question that has probably already been asked: > > =A0 > > I would like to apply the find() function on a bidimensional array and=20= > receive as output the bidimensional indices for the elements matching=20= > the condition. This is easy in Matlab but doesn't seem to be possible=20= > with Python. > > =A0 > > Here is a short example (in Matlab code) of what I would like to have: > > =A0 > > >> z=3Drand(3,3) > > =A0 > > z =3D > > =A0 > > =A0=A0=A0 0.9501=A0=A0=A0 0.4860=A0=A0=A0 0.4565 > > =A0=A0=A0 0.2311=A0=A0=A0 0.8913=A0=A0=A0 0.0185 > > =A0=A0=A0 0.6068=A0=A0=A0 0.7621=A0=A0=A0 0.8214 > > =A0 > > >> [i,j]=3Dfind(z<0.5) > > =A0 > > i =3D > > =A0 > > =A0=A0=A0=A0 2 > > =A0=A0=A0=A0 1 > > =A0=A0=A0=A0 1 > > =A0=A0=A0=A0 2 > > =A0 > > =A0 > > j =3D > > =A0 > > =A0=A0=A0=A0 1 > > =A0=A0=A0=A0 2 > > =A0=A0=A0=A0 3 > > =A0=A0=A0=A0 3 > > =A0 > > A solution for me? > > =A0 > > Thank you, > > =A0 > > Dimitri D'Or > > =A0 |
From: John H. <jdh...@ac...> - 2005-05-17 15:30:57
|
>>>>> "Perry" == Perry Greenfield <pe...@st...> writes: Perry> It's simpler in numarray: Perry> i,j = where(z<0.5) Perry> For numeric it's more work: Perry> ind = where(z.flat < 0.5) i = ind//z.shape[1] j = ind % Perry> z.shape[1] Perry> One could turn this into a function for Numeric (to handle Perry> all dimensionalities, etc) I think you meant to use "nonzero" rather than "where" for Numeric... from matplotlib.numerix import where, nonzero from matplotlib.numerix.mlab import rand z = rand(4,5) # It's simpler in numarray: i,j = where(z<0.5) print i,j # For numeric it's more work: ind = nonzero(z.flat < 0.5) i = ind//z.shape[1] j = ind % z.shape[1] print i,j I also think wrapping this functionality into the find function in mpl is a good idea... I frequently miss it. JDH |
From: Perry G. <pe...@st...> - 2005-05-17 15:44:12
|
On May 17, 2005, at 11:30 AM, John Hunter wrote: >>>>>> "Perry" == Perry Greenfield <pe...@st...> writes: > > Perry> It's simpler in numarray: > > Perry> i,j = where(z<0.5) > > Perry> For numeric it's more work: > > Perry> ind = where(z.flat < 0.5) i = ind//z.shape[1] j = ind % > Perry> z.shape[1] > > Perry> One could turn this into a function for Numeric (to handle > Perry> all dimensionalities, etc) > > I think you meant to use "nonzero" rather than "where" for Numeric... Yes, indeed. |
From: John H. <jdh...@ac...> - 2005-05-17 15:50:52
|
>>>>> "Perry" == Perry Greenfield <pe...@st...> writes: Perry> Yes, indeed. I know you don't want to get into the business of porting numarray functionality back to Numeric :-) but do you know of an easy way to generalize this to N dimensions for Numeric. I took a brief look at how numarray did it, and it relies on choose under the hood, but it didn't look like it would be straightforward to port to Numeric. An Nd "find" would be useful in mpl, at least until the whole array object scenario stabilizes. JDH |
From: Perry G. <pe...@st...> - 2005-05-17 16:16:25
|
On May 17, 2005, at 11:50 AM, John Hunter wrote: >>>>>> "Perry" == Perry Greenfield <pe...@st...> writes: > > Perry> Yes, indeed. > > I know you don't want to get into the business of porting numarray > functionality back to Numeric :-) but do you know of an easy way to > generalize this to N dimensions for Numeric. I took a brief look at > how numarray did it, and it relies on choose under the hood, but it > didn't look like it would be straightforward to port to Numeric. > > An Nd "find" would be useful in mpl, at least until the whole array > object scenario stabilizes. > > JDH numarray does it at a low level which wouldn't be easy to duplicate in Numeric. But I don't think it is that hard to do in Python so long as you don't mind using ravel() and generating an intermediate index array. Something like (not checked or tested, surely there's a mistake in it somewhere)? def find(condition): ind = nonzero(ravel(condition)) cshape = multiply.accumulate(condition.shape)[-1:0:-1] indices = [] # probably better done as a list comprehension but I'm too lazy to look it up for size in cshape: indices.append(ind//size) indices.append(ind % condition.shape[-1] return tuple(indices) But I may be missing a complication. Perry |