检查矩阵中的相邻数字

时间:2016-04-21 19:43:06

标签: c# matrix

如何计算3x3矩阵中的所有相邻矩阵,看起来像这样

1  1  1
1  0  0
0  0  0

我想要的输出是(在方括号中是索引[x,y],我只关心数字,索引是准确的) [0,0] - 2,[1,0] - 3,[2,0] - 1,[0,1] -2等......我不计算中间的数字。

我有一个大矩阵,任务是循环遍历每个数字,想象这个矩阵并计算中间数字的数量。 这是我的代码,但我无法使其工作。

这是我的方法:

public int CheckArea(int cordX, int cordY)
    {
        int count = 0;
        for (int i = cordX - 1; i <= cordX + 1; i++)
        {
            for (int j = cordY - 1; j <= cordY + 1; j++)
            {
                if (j != cordY && i != cordX && tileField[i, j])
                {
                    count++;
                }
            }
        }
       return count;      
    }

它不应该是一个索引问题我设置这样的矩阵:

        tileField = new Boolean[width, height];
        for (int x = 0; x < width; x++)
        {
            for (int y = 0; y < height; y++)
            {
                tileField[x, y] = false;
            }
        }

但是我不打印第一个和最后一个列和行,所以我的Print方法是:

public void Print()
    {
        for (int x = 1; x < this.width-1 ; x++)
        {
            for (int y = 1; y < this.height -1; y++)
            {
                if (!tileField[x, y])
                {
                    Console.Write("0 ");
                }
                else
                {
                    Console.Write("1 ");
                }
            }
            Console.WriteLine("");
        }

    }

4 个答案:

答案 0 :(得分:2)

虽然看起来这似乎是因为使用循环来减少代码,但您可能只想手动检查每个邻居,因为只有8种可能性。下面的解决方案假设您对网格周围的“包裹”并不感兴趣,并且除了要检查的坐标之外,字段中没有任何其他行/列(如顶部和左侧的缓冲行和列)边缘)。我建议做以下事情:

public int CheckArea(int cordX, int cordY)
{
    int count = 0;
    int fieldWidth = tileField.GetLength(1);
    int fieldHeight = tileField.GetLength(0);

    // Check for the neighbor to the North
    if(cordY != 0)
    {
        count += GetValueAtCoordinate(cordX,cordY - 1);
    }
    // Check for the neighbor to the East
    if (cordX < fieldWidth - 1)
    {
        count += GetValueAtCoordinate(cordX + 1, cordY);

        // NE neighbor
        if(cordY != 0)
        {
            count += GetValueAtCoordinate(cordX - 1, cordY - 1);
        }

        // SE neighbor
        if(cordY != fieldWidth - 1)
        {
            count += GetValueAtCoordinate(cordX - 1, cordY + 1);
        }
    }
    // Check for the neighbor to the South
    if (cordY < fieldHeight - 1)
    {
        count += GetValueAtCoordinate(cordX, cordY + 1);
    }
    // Check for the neighbor to the West
    if (cordX != 0)
    {
        count += GetValueAtCoordinate(cordX - 1, cordY);

        // NW neighbor
        if(cordY != 0)
        {
            count += GetValueAtCoordinate(cordX - 1, cordY - 1);
        }

        // SW neighbor
        if(cordY != fieldHeight - 1)
        {
            count += GetValueAtCoordinate(cordX - 1, cordY + 1);
        }
    }

    return count;
}

public int GetValueAtCoordinate(int x, int y)
{
    return tileField[x,y] == true ? 1 : 0;
}

您可以使用各种其他方法,但除非有特定原因您不想使用检查每个邻居的简单路径,我认为您不会真正节省任何时间的循环。我还添加了一个方法,它将给定坐标处的bool值转换为int为1表示true,0表示false表示您的矩阵类型为bools。

修改 如果将其视为1索引数组,只需将使用fieldWidth和fieldHeight变量的边缘检查更改为不减1。

答案 1 :(得分:1)

//dx and dy contains a vector of all possible moves: E, NE, N, NW, W, SW, S, SE.
static int [] dx = {1, 1, 0,-1,-1,-1,  0,  1};
static int [] dy = {0, 1, 1, 1, 0,-1, -1, -1};


public int CheckArea(int cordX, int cordY) //i assumed that cordX and cordY was a row and a column of the matrix
{

    int count = 0;
    for(int k = 0; k < dx.length; k++){
        int i = dx[k] + cordX;
        int j = dy[k] + cordY;
        if(canCount(i,j, matrix) && matrix[i][j] == 1){
           count++; 
        }
    }



    return count;      
}
//To check that you are within the bounds of the matrix
public boolean canCount(int i, int j, int [][] matrix){
    return i >= 0 && i < matrix.length && j >= 0 && j < matrix[0].length;
}

答案 2 :(得分:0)

以下方法返回一个2D数组,表示其中的数量 请注意,在此数组中,元素的null表示当前元素不是一个。

Mammal


def split(original_list, weight_list):
    sublists = []
    prev_index = 0
    for weight in weight_list:
        next_index = prev_index + math.ceil( (len(my_list) * weight) )

        sublists.append( my_list[prev_index : next_index] )
        prev_index = next_index

    return sublists

## function call ##
my_list = [...] # whatever your list contains
weight_list = [0.2, 0.3, 0.5] # This equals to your 20%, 30% and 50%

sublists = split(my_list, weight_list)


结果(private int?[,] matrixOnesCount(int[,] matrixInput) { int?[,] matrixOutput = new int?[matrixInput.GetLength(0), matrixInput.GetLength(1)]; int rMax = matrixInput.GetLength(0); int cMax = matrixInput.GetLength(1); for (int r = 0; r < rMax; r++) for (int c = 0; c < cMax; c++) { if (matrixInput[r, c] != 1) { matrixOutput[r, c] = null; } else { int numOfOneInNeighbors = 0; numOfOneInNeighbors += (r - 1 >= 0) ? matrixInput[r - 1, c] : 0; // Top neighborhood numOfOneInNeighbors += (r - 1 >= 0 && c - 1 >= 0) ? matrixInput[r - 1, c - 1] : 0; // Top-Left neighborhood numOfOneInNeighbors += (c - 1 >= 0) ? matrixInput[r, c - 1] : 0; // Left neighborhood numOfOneInNeighbors += (r + 1 < rMax && c - 1 >= 0) ? matrixInput[r + 1, c - 1] : 0; // Buttom-Left neighborhood numOfOneInNeighbors += (r + 1 < rMax) ? matrixInput[r + 1, c] : 0; // Buttom neighborhood numOfOneInNeighbors += (r + 1 < rMax && c + 1 < cMax) ? matrixInput[r + 1, c + 1] : 0; // Buttom-Right neighborhood numOfOneInNeighbors += (c + 1 < cMax) ? matrixInput[r, c + 1] : 0; // Right neighborhood numOfOneInNeighbors += (r - 1 >= 0 && c + 1 < cMax) ? matrixInput[r - 1, c + 1] : 0; // Top-Right neighborhood matrixOutput[r, c] = numOfOneInNeighbors; } } return matrixOutput; } )是:

int[,] matrixInput = { { 1, 0, 1 },
                        { 1, 0, 1 },
                        { 1, 1, 0 },
                        { 0, 1, 0 } };

int?[,] matrixOutput = matrixOnesCount(matrixInput);

答案 3 :(得分:0)

或者您可以检查i和j是否在CheckArea的矩阵中,如下所示:

public int CheckArea(int cordX, int cordY)
{
    int count = 0, matrixLenght = 3;//works for any matrix as long as you change the lenght
    for (int i = cordX - 1; i <= cordX + 1; i++)
    {
        for (int j = cordY - 1; j <= cordY + 1; j++)
        {
            (i >= 0 && i < matrixLenght  && j >= 0 && j < matrixLenght  && (i != x || j != y))
            {
                count++;
            }
        }
    }
   return count;      
}