发现从矩阵中的所有局部最大 [英] find all local maximum from the matrix
问题描述
请用Java编写的方法将接收输入矩阵(INT [] []矩阵),哪些应该找到从矩阵的所有局部最大。甲局部最大值是这样的一些在基质大于它的所有近邻。该方法应该返回找到的所有本地最大数目的位置的列表。
我想这code键使这一点,但不知道这种想法是正确与否的code
私有静态列表<整数GT; findLocal(中间体[] []矩阵)
{
名单<整数GT;当地人=新的ArrayList<整数GT;();
的for(int i = 0; I< matrix.length;我++){
对于(INT J = 0; J<基质[0] .length; J ++){
如果(ⅰ&所述; matrix.length - 1和;&安培; J&所述基质[0] .length - 1){
如果(矩阵[I] [j]的&其中;矩阵[I + 1] [j]的&安培;&安培;矩阵[I] [j]的&其中;矩阵[I] [J + 1]安培;&安培;矩阵[i]于[J] LT;基质[I + 1] [J + 1]){
locals.add(I + J);
} 其他 {
}
}
}
}
返回本地人;
}
您写
如果(矩阵[i] [j]的<基质[I + 1] [J]。
&功放;&安培;矩阵[i] [j]的<矩阵[I] [J + 1]
&功放;&安培;矩阵[i] [j]的<矩阵[I + 1] [J + 1]){
这将检查元素是否超过三个邻国较小。那么其他五个邻居?让我们来看看他们吧:
如果(矩阵[i] [j]的<基质[I + 1] [J]。
&功放;&安培;矩阵[i] [j]的<矩阵[I + 1] [J-1]
&功放;&安培;矩阵[i] [j]的<矩阵[I + 1] [J + 1]
&功放;&安培;矩阵[i] [j]的<矩阵[I] [J + 1]
&功放;&安培;矩阵[i] [j]的<矩阵[I] [J - 1]
&功放;&安培;矩阵[i] [j]的<矩阵[I-1] [J-1])
&功放;&安培;矩阵[i] [j]的<矩阵[I-1] [j]的)
&功放;&安培;矩阵[i] [j]的<矩阵[I-1] [J + 1]){
此假设你要检查的元素的8近邻,即直线和斜线。调整,如果这是不是你想要的。
另外的要求是找到当地的最大值。这标识局部最小值。更改<
到>
。
另一件事是, locals.add(I + J)
并没有做什么,你认为它。如果元(I = 3,J = 4)是当地最大的,那么你说 locals.add(7)
,这显然不是你想要的。
Please write a method in Java which will receive as input a matrix (int[][] matrix) and which should find all local maximum from the matrix. A local maximum is such a number in the matrix that is greater than all its immediate neighbors. The method should return the List of locations of all local maximum numbers found.
i tried this code to make this but don't know if this idea is correct or not the code
private static List<Integer> findLocal(int[][] matrix)
{
List<Integer> locals = new ArrayList<Integer>();
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[0].length; j++) {
if (i < matrix.length - 1 && j < matrix[0].length - 1) {
if (matrix[i][j] < matrix[i + 1][j] && matrix[i][j] < matrix[i][j + 1] && matrix[i][j] < matrix[i + 1][j + 1]) {
locals.add(i + j);
} else {
}
}
}
}
return locals;
}
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.
这篇关于发现从矩阵中的所有局部最大的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!