【发布时间】:2020-05-01 20:08:01
【问题描述】:
假设我有这个二维数组,我们称之为矩阵
float[,] arr = { {0, 2, 7 },
{3, 1, 0 },
{6, 2, 4 } };
我必须创建一个函数,你给出两个整数 i 和 j,它会返回一个没有 i 行和 j 列的矩阵。例如,如果我将 (0,0) 作为参数传递,我将得到
float [,] newArr = {{1, 0 },
{2, 4 } }
因为第 0 行和第 0 列被删除了。
我自己想出的解决方案是这个功能可以正常工作并完成它应该做的事情,但我想要一个更小、更紧凑、更有意义的解决方案。
public Matrix subMatrix(int _i, int _j)
{
Matrix result = new Matrix(_rows - 1, _cols - 1);
try
{
if (isSquare())
{
List<float> listMatrix = new List<float>();
for (int i = 0; i < _rows; i++)
for (int j = 0; j < _cols; j++)
{
if (i != _i && j != _j)
{
listMatrix.Add(matrix[i, j]);
}
}
int x = -1;
int y = 0;
int z = 0;
foreach (var item in listMatrix)
{
if (y % (result._rows) == 0)
{
x++;
y = 0;
}
result.matrix[x, y] = item;
y++;
}
}
}
catch (IndexOutOfRangeException e)
{
throw e;
}
catch (ArgumentOutOfRangeException e)
{
throw e;
}
return result;
}
BTW Matrix 是我制作的一个类,它基本上是一个二维数组
【问题讨论】: