Finding local minima in a multidimensional array

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Finding local minima in a multidimensional array

BGreen
I would like to find all local minima in an array. For a 2D array M, I would want to find (i,j) such that:
M(i,j)<M(i,j+1)
M(i,j)<M(i,j-1)
M(i,j)<M(i+1,j)
M(i,j)<M(i -1,j).
I would like to do this in higher dimensions as well. This seems similar to the sort of thing that could be done with the "find" function. I've seen find used for elementwise operations, but I don't know whether it can be used for comparisons that require accessing two elements of the array which is passed in. Is that possible?

While it's easy enough to write loops to do this for 1 or 2 dimensions, it would be nice to have a more general method.

- Brett Green


Reply | Threaded
Open this post in threaded view
|

Re: Finding local minima in a multidimensional array

Juan Pablo Carbajal-2
Hi Brett,

Did you solve your problem?
It seems the definition of the minima you are looking for is
problematic, in 1D it reads

# assume M exist with length N
i = 2:(N - 1);
ismin = (M(i) < M(i + 1)) & (M(i) < M(i - 1))
# these are the location of minima
loc = find (ismin) + 1

But check this
M = [1 2 3 0 0 5 6];

gives you no minima. Is that alright?

On Sat, Nov 9, 2019 at 7:01 PM Brett Green <[hidden email]> wrote:

>
> I would like to find all local minima in an array. For a 2D array M, I would want to find (i,j) such that:
> M(i,j)<M(i,j+1)
> M(i,j)<M(i,j-1)
> M(i,j)<M(i+1,j)
> M(i,j)<M(i -1,j).
> I would like to do this in higher dimensions as well. This seems similar to the sort of thing that could be done with the "find" function. I've seen find used for elementwise operations, but I don't know whether it can be used for comparisons that require accessing two elements of the array which is passed in. Is that possible?
>
> While it's easy enough to write loops to do this for 1 or 2 dimensions, it would be nice to have a more general method.
>
> - Brett Green
>


Reply | Threaded
Open this post in threaded view
|

Re: Finding local minima in a multidimensional array

BGreen
On Wed, Nov 20, 2019 at 9:04 AM Juan Pablo Carbajal <[hidden email]> wrote:
Hi Brett,

Did you solve your problem?
It seems the definition of the minima you are looking for is
problematic, in 1D it reads

# assume M exist with length N
i = 2:(N - 1);
ismin = (M(i) < M(i + 1)) & (M(i) < M(i - 1))
# these are the location of minima
loc = find (ismin) + 1

But check this
M = [1 2 3 0 0 5 6];

gives you no minima. Is that alright?

Thank you! Actually, several days after asking here I also asked on Stack Overflow. It turns out Octave does indeed already have a function, imregionalmin, that serves this purpose in the image package. Most importantly, imregionalmin works for an array of arbitrary dimensions.

Also, it will find the "extended" minima of adjacent equal values. It also includes minima on the borders, but if that were undesirable, one could always just throw away the first and last entry for each index.

pkg load image
M = [1 2 3 0 0 5 6];
imregionalmin(M)

returns

ans =
  1  0  0  1  1  0  0