我认为你的意思是使用 LINQ
public double[][] GetSubMatrix(int row, int col, int row_count, int col_count)
{
return elements.Skip(row).Take(row_count).Select(
(each_row) => each_row.Skip(col).Take(col_count).ToArray()).ToArray();
}
我对矩阵结构的偏好是在没有 LINQ 的情况下使用交错数组。您不能使用 LINQ 轻松地在数组上设置值,而且命令在一段时间后确实会变得很长而且很愚蠢。
我确实尽可能使用Array.Copy,而不是内部循环。
试试这个Matrix泛型类示例:
public class Matrix<T>
{
readonly int rows, cols;
readonly T[][] elements;
public Matrix(int rows, int cols)
{
this.rows=rows;
this.cols=cols;
this.elements=new T[rows][];
for(int i = 0; i<rows; i++)
{
elements[i]=new T[cols];
}
}
public int Rows { get { return rows; } }
public int Columns { get { return cols; } }
public T this[int row, int col]
{
get { return elements[row][col]; }
set { elements[row][col]=value; }
}
public T[] GetRow(int row) { return elements[row]; } // This is fast
public T[] GetColumn(int col) // This is slow
{
T[] column = new T[rows];
for(int i = 0; i<rows; i++)
{
column[i]=elements[i][col];
}
return column;
}
public T[][] GetSubMatrix(int row, int col, int row_count, int col_count)
{
T[][] result = new T[row_count][];
for(int i = 0; i<row_count; i++)
{
// This is fast
Array.Copy(elements[row+i], col, result[i], 0, col_count);
}
return result;
}
public void SetSubMatrix(int row, int col, T[][] matrix)
{
int row_count = matrix.Length;
int col_count = row_count>0 ? matrix[0].Length : 0;
for(int i = 0; i<row_count; i++)
{
// This is fast
Array.Copy(matrix[i], 0, elements[row+i], col, col_count);
}
}
}