如何在C#中的3x3矩阵中获得所有可能的2x2子矩阵?

时间:2017-10-22 07:07:23

标签: c# matrix multidimensional-array

如果矩阵A的大小(3x3),那么我应该使用查找行列式的方法,比如抓取第一个元素的行和列并将其从数组2D数组中移除以获取剩余的元素然后移动到下一个元素并重复相同的步骤?

[{1,2,3},
 {4,5,6},
 {7,8,9}]

3 个答案:

答案 0 :(得分:0)

我终于能够做到了,这就是我所做的: enter image description here

class program
{
    public static void Main()
    {
        int[,] arr = new int[3, 3];
        Console.WriteLine("Enter elements of " + (arr.GetUpperBound(0) + 1) + "x" + (arr.GetUpperBound(1) + 1) + " matrix:");
        for (int i = 0; i < (arr.GetUpperBound(0) + 1); i++)
        {
            for (int j = 0; j < (arr.GetUpperBound(1) + 1); j++)
            {
                arr[i, j] = Convert.ToInt32(Console.ReadLine());
            }
        }
        Console.WriteLine("Matrix entered: ");
        for (int i = 0; i < (arr.GetUpperBound(0) + 1); i++)
        {
            for (int j = 0; j < (arr.GetUpperBound(1) + 1); j++)
            {
                Console.Write("\t" + arr[i, j]);
            }
            Console.WriteLine();
        }
        Console.WriteLine("Possible sub-matrices: ");
        for (int i = 0; i < 3; i++)
        {
           for (int j = 0; j< 3; j++)
             {
                TrimArray(i,j,arr);
             }    
        }

    }
    public static int[,] TrimArray(int row, int column, int[,] original)
    {
        int[,] resultant = new int[original.GetLength(0) - 1, original.GetLength(1) - 1];

        for (int i = 0, j = 0; i < original.GetLength(0); i++)
        {
            if (i == row)
                continue;

            for (int k = 0, u = 0; k < original.GetLength(1); k++)
            {
                if (k == column)
                    continue;

                resultant[j, u] = original[i, k];
                u++;
            }
            j++;
        }
        Console.WriteLine();
        for (int i = 0; i < 2; i++)
        {
                for (int j = 0; j< 2; j++)
               {
                    Console.Write("\t"+resultant[i,j]);
               }
                Console.WriteLine();
        }
        return resultant;
    }
} 

答案 1 :(得分:0)

我昨天为你做了这个,我创建了一个方法,它将返回一个方形矩阵,给定一个父矩阵和长度。

    static void Main(string[] args)
    {
        int[][] parentMatrix = new int[][]
        {
            new int [] { 1, 2, 3 },
            new int [] { 4, 5, 6 },
            new int [] { 7, 8, 9 }
        };

        var chunks = GetSubMatrices(parentMatrix, 2);

        Console.WriteLine(chunks);
    }



    static List<int[][]> GetSubMatrices(int[][] parentMatrix, int m)
    {
        int n = parentMatrix.Length > m ? parentMatrix.Length : throw new InvalidOperationException("You can't use a matrix smaller than the chunk size");

        var chunks = new List<int[][]>();
        int movLimit = n - m + 1;

        var allCount = Math.Pow(movLimit, 2);


        for (int selRow = 0; selRow < movLimit; selRow ++)
        {
            for (int selCol = 0; selCol < movLimit; selCol ++)
            {
                // this is start position of the chunk
                var chunk = new int[m][];

                for (int row = 0; row < m; row++)
                {
                    chunk[row] = new int[m];
                    for (int col = 0; col < m; col++)
                    {
                        chunk[row][col] = parentMatrix[selRow + row][selCol + col];
                    }
                }

                chunks.Add(chunk);
            }
        }

        return chunks;
    }

如果您在使用它时遇到任何问题,可以在下方进行评论。

答案 2 :(得分:0)

我需要解决类似的问题,并提出了这个答案。希望它能增加您的答案库。如果指定的子矩阵不大于1,则不执行任何操作。

public static void GetSubMatrixes(int[,] arr, int size)
    {
        int parentMatrixRowLength = arr.GetLength(0);
        int parentMatrixColLength = arr.GetLength(1);

        var overall = new List<object>();

        if(size > 1)
        {
            for (int i = 0; i < parentMatrixRowLength; i++)
            {
                //get the columns
                for (int j = 0; j < parentMatrixColLength; j++)
                {
                    var subMatrix = new int[size, size];

                    /*if the new matrix starts from second to the last value in either the row(horizontal or column)
                     * do not proceed, go to the row or column in the parent matrix
                     * */
                    if (j < parentMatrixColLength - (size - 1) && i < parentMatrixRowLength - (size - 1))
                    {
                        //add 
                        for (int m = 0; m < subMatrix.GetLength(0); m++)
                        {
                            for (int n = 0; n < subMatrix.GetLength(1); n++)
                            {
                                /*check the sum of current column value and the sum of the current row value 
                                 * of the parent column length and row length if it goes out of bounds
                                 */
                                var row = i + m; var col = j + n;

                                //actual check here
                                if (row < parentMatrixRowLength && col < parentMatrixColLength)
                                {
                                    subMatrix[m, n] = arr[i + m, j + n];
                                }
                            }
                        }
                        overall.Add(subMatrix);
                    }
                }
            }

            //display the sub matrixes here
            for (int i = 0; i < overall.Count; i++)
            {
                var matrix = overall[i] as int[,];

                for (int y = 0; y < matrix.GetLength(0); y++)
                {
                    for (int x = 0; x < matrix.GetLength(1); x++)
                    {
                        Console.Write(string.Format("{0} ", matrix[y, x]));
                    }
                    Console.Write(Environment.NewLine + Environment.NewLine);
                }

                Console.WriteLine();
            }
        }
    }
相关问题