Arrays 如何为可变大小的数组编写大量if语句

Arrays 如何为可变大小的数组编写大量if语句,arrays,algorithm,Arrays,Algorithm,我有一些代码,可以从一个大数组中提取重复的局部邻域。我可以设置代码,为我提供3x3个邻居,或任何其他大小的邻居,如5x5或11x11。然后,我需要检查有关阵列的各种情况,如下所述。我已经开始用大量嵌套的if语句编写它,但我认为一定有更好的方法 0 1 2 3 4 -------------- 0 |1 2 3 4 5 1 |6 7 8 9 10 2 |11 12 13 14 15 3 |16 17 18 19 20 4 |21 22 23 24 25 给定上

我有一些代码,可以从一个大数组中提取重复的局部邻域。我可以设置代码,为我提供3x3个邻居,或任何其他大小的邻居,如5x5或11x11。然后,我需要检查有关阵列的各种情况,如下所述。我已经开始用大量嵌套的if语句编写它,但我认为一定有更好的方法

   0  1  2  3  4
   --------------
0 |1  2  3  4  5
1 |6  7  8  9  10
2 |11 12 13 14 15
3 |16 17 18 19 20
4 |21 22 23 24 25
给定上面的数组,我想检查[0,2]和[1,2]中的值是否小于阈值,[3,2]和[4,2]中的值是否大于阈值。然后我想用一条穿过中心的垂直线(而不是我给出的例子中的水平线)做同样的事情,两条对角线也是一样的


目前,如果没有大量的if语句,我看不到任何方法可以做到这一点,这些语句会变得非常混乱,并且很快就很难维护。我确信一定有更好的方法来做这件事——有什么想法吗?

我想这取决于你的编程模式

例如,在Mathematica等列表编程环境中,您可以执行以下操作:

f[x_,bound_] :=
 With[{d = (IntegerPart[Dimensions[x][[1]]/2] + 1)},
  And @@ Flatten[
    Table[{x[[d, i]]     > bound, 
           x[[d, i + d]] < bound, 
           x[[i, d]]     > bound, 
           x[[i + d, d]] < bound}, 
    {i, d - 1}]]]  
f[]如果满足条件,则返回True,否则返回False


代码不是最优的,因为它不会使AND短路,并在结束时继续求值,然后对所有结果进行AND。这使得代码更易于维护。根据您的性能要求,在您的情况下可能(或不可能)实现这些功能。

创建一个可提取一维向量的帮助器。然后做一些类似的事情:

vector v1 = array.row(2);
vector v2 = array.column(2);
vector v3 = array.diagonal(top left to bottom right);
vector v4 = array.diagonal(bottom left to top right).
然后编写一个函数,该函数接受这些新向量中的一个,并检查您的条件。它可能有ifs之类的,但不会太多

return (vec[0] and vec[1] < threshold) and 
       (vec[3] and vec[4] > threshold)

列出要检查的三元组列表/数组:

trips = [[X1, Y1, BOUND1], [X2, Y2, BOUND2], ..., [Xn, Yn, BOUNDn]]
然后在
trips
数组中循环,并检查矩阵中的每一个
M

使用java,下面是检查所有东西是否满足某个下限的代码:

private boolean boundsCheck(int[][] M, int[][] trips)
{
   for (int[] trip : trips)
   {
      if (M[trip[0]][trip[1]] > trip[2]) return false;
   }
   return true;
}
同样的类型也可以扩展以检查上界:

private boolean boundsCheck(int[][] M, int[][] tripsLower, int[][] tripsUpper)
{
   for (int[] trip : tripsLower)
   {
      if (M[trip[0]][trip[1]] > trip[2]) return false;
   }
   for (int[] trip : tripsUpper)
   {
      if (M[trip[0]][trip[1]] < trip[2]) return false;
   }
   return true;
}
private boolean boundsCheck(int[][]M,int[][]tripSleer,int[][]tripSuper)
{
对于(int[]行程:TripSleer)
{
如果(M[trip[0]][trip[1]]>trip[2])返回false;
}
对于(int[]行程:TripSupery)
{
如果(M[trip[0]][trip[1]]
这将从要检查的元素中考虑执行检查的代码。
它允许您以任意方式生成要检查的元素及其边界:以编程方式或硬编码方式。如果以硬编码结束,至少代码看起来很简单:它只是一个数组声明。

您可以使用一个函数指针数组来指定每个平方需要检查的内容

int a = ...;
int b = ...;
bool bigger_than_a(int x) { return x > a; }
bool smaller_than_b(int x) { return x < b; }

bool (*)(int) check_functions[5][5] = {
    NULL, bigger_than_a, bigger_than_b, NULL, NULL,
    ...
}

int x_start = ...;
int y_start = ...;
for (int x = 0; x < 5; x++) {
  for (int y = 0; y < 5; y++) {
    if (check_functions[x][y] != NULL &&
        !check_functions[x][y](array[x_start + x][y_start + y]) {
           ...check failed...
    }
  }
}
inta=。。。;
int b=。。。;
bool大于a(int x){return x>a;}
bool小于b(int x){return x

如果您需要不同的大小,只需为每个大小制作一个不同的
检查函数数组。

循环和额外边界检查的
有什么问题吗?您如何处理矩阵边缘的大小写,是环绕到数组的另一侧还是在边缘处切掉正方形?我编写的代码用于提取se local Neighbories在提取小数组时会重复大数组边缘的值,以确保小数组始终具有指定的大小。我可以使用一些for循环,但我不知道如何使用它们,以便在处理任何大小的本地数组的同时,尽可能少地重复代码(3x3、5x5等)。检查元素的模式是什么?
private boolean boundsCheck(int[][] M, int[][] tripsLower, int[][] tripsUpper)
{
   for (int[] trip : tripsLower)
   {
      if (M[trip[0]][trip[1]] > trip[2]) return false;
   }
   for (int[] trip : tripsUpper)
   {
      if (M[trip[0]][trip[1]] < trip[2]) return false;
   }
   return true;
}
int a = ...;
int b = ...;
bool bigger_than_a(int x) { return x > a; }
bool smaller_than_b(int x) { return x < b; }

bool (*)(int) check_functions[5][5] = {
    NULL, bigger_than_a, bigger_than_b, NULL, NULL,
    ...
}

int x_start = ...;
int y_start = ...;
for (int x = 0; x < 5; x++) {
  for (int y = 0; y < 5; y++) {
    if (check_functions[x][y] != NULL &&
        !check_functions[x][y](array[x_start + x][y_start + y]) {
           ...check failed...
    }
  }
}