从矩阵中找到所有局部最大值

发布于 2024-11-30 21:12:42 字数 1469 浏览 1 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

回心转意 2024-12-07 21:12:42

好吧,现在看来你已经有了一个想法,而且它基本上是一个可行的想法。

它只有一个缺陷:您正在查看的邻域是错误的:对于点 (i,j),(冯诺依曼)邻居是 (i+1,j)、(i,j+1)、(i-1) ,j)和(i,j-1)。

因此,当您检查这一点时,它通常应该有效,但是:您必须特别注意边界。由于您没有 -1 列/行,也没有第 n+1 列/行,所以当您在那里时,请不考虑该邻居点。你如何处理它,取决于你想如何对待它:邻域是否应该环绕,边界是否应该具有恒定值,是否应该被视为-infty?

Ok, now it seems you have come with an idea up, and it is basically a working idea.

It has just one flaw: The neigborhood your are looking at is wrong: for a point (i,j) the (von Neumann) neighbors are (i+1,j), (i,j+1), (i-1, j), and (i,j-1).

So when you check for this point it should in general work, BUT: You have to take special care of the borders. As you have no -1st and no n+1th column/row you have when you are there take that neighbor point out of consideration. How you handle it, depends on how you want to treat it: Should the neighborhood wrap around, should the border have a constant value, should it be treated as -infty?

逆光飞翔i 2024-12-07 21:12:42

您可以编写

if (matrix[i][j] < matrix[i + 1][j] 
    && matrix[i][j] < matrix[i][j + 1] 
    && matrix[i][j] < matrix[i + 1][j + 1]) {

检查该元素是否小于其三个邻居的元素。那么其他五个邻居呢?让我们也检查一下它们:

if (matrix[i][j] < matrix[i + 1][j] 
    && matrix[i][j] < matrix[i+1][j-1] 
    && matrix[i][j] < matrix[i+1][j+1] 
    && matrix[i][j] < matrix[i][j + 1] 
    && matrix[i][j] < matrix[i][j - 1] 
    && matrix[i][j] < matrix[i-1][j-1]) 
    && matrix[i][j] < matrix[i-1][j]) 
    && matrix[i][j] < matrix[i-1][j+1]) {

这假设您想要检查元素的 8 个直接邻居,即直线和对角线。如果这不是您想要的,请进行调整。

还要求找到局部最大值。这确定了局部最小值。将 < 更改为 >

另一件事是 locals.add(i + j) 并没有像你想象的那样做。如果元素 (i=3,j=4) 是局部最大值,那么您所说的是 locals.add(7),这显然不是您想要的。

You write

if (matrix[i][j] < matrix[i + 1][j] 
    && matrix[i][j] < matrix[i][j + 1] 
    && matrix[i][j] < matrix[i + 1][j + 1]) {

which checks for whether the element is smaller than three of its neighbours. What about the other five neighbours? Let's check them, too:

if (matrix[i][j] < matrix[i + 1][j] 
    && matrix[i][j] < matrix[i+1][j-1] 
    && matrix[i][j] < matrix[i+1][j+1] 
    && matrix[i][j] < matrix[i][j + 1] 
    && matrix[i][j] < matrix[i][j - 1] 
    && matrix[i][j] < matrix[i-1][j-1]) 
    && matrix[i][j] < matrix[i-1][j]) 
    && matrix[i][j] < matrix[i-1][j+1]) {

This assumes you want to check the element's 8 immediate neighbours, i.e. straight and diagonal. Adjust if this is not what you want.

Also the requirement is finding the local maxima. This identifies a local minimum. Change < to >.

Another thing is that locals.add(i + j) doesn't do what you think it does. If element (i=3,j=4) is a local maximum, then you're saying locals.add(7), which clearly is not what you want.

孤城病女 2024-12-07 21:12:42

我认为没有必要对每个矩阵进行8​​次比较
元素。您应该使用两个额外的数组:

  1. MATRIX[n+2][n+2]:它将包含您的输入矩阵以及一个额外的数组
    所有 INT_MIN 的层(因此,每个矩阵元素都具有所有 8
    邻居)。

  2. Mark[n+2][n+2] = {0}(您只需访问
    未标记的输入矩阵)如果矩阵元素是
    声明为局部最小值,您应该将其所有邻居标记为
    Mark[][] 向量。

这样做,复杂度仍然是 O(n^2),但不是。的比较将
最小化。

I think that there is no need for 8 comparisons for every matrix
element. You should take two extra arrays:

  1. MATRIX[n+2][n+2]: It will contain yout input matrix with an extra
    layer of all INT_MIN's(So, that every matrix element has all 8
    neighbours).

  2. Mark[n+2][n+2] = {0} (You should only have to visit the elements in
    the input matrix which are not marked) If a matrix element is
    declared as a local minimum, you should marks all its neighbour in
    the Mark[][] vector.

By doing so, complexity will remain O(n^2) but no. of comparisons will
be minimised.

羞稚 2024-12-07 21:12:42

您必须分别存储 i 和 j 才能正确记住元素的位置。因此,

List<Integer> locals = new ArrayList<Integer>();

不会起作用。
你可以用它

 List<Integer[]> locals = new ArrayList<Integer[]>();

来代替。要添加新发现的局部最大值的位置,请使用

 Integer[] localMax = {i, j};
 locals.add(localMax);

You'll have to store i and j separately to remember the position of an element correctly. Thus,

List<Integer> locals = new ArrayList<Integer>();

won't work.
You could use

 List<Integer[]> locals = new ArrayList<Integer[]>();

instead. And to add the position of a newly found local maximum, use

 Integer[] localMax = {i, j};
 locals.add(localMax);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文