我有一个对象,叫做(暂且这么称呼)MatricesMatrix
,它是一个包含子矩阵的矩阵(每个子矩阵都以double[,]
形式存储)。我想将所有内部矩阵中的值提取出来,组成一个大矩阵。以下是我目前的代码:
public Matrix ConvertToMatrix()
{
//Figure out how big the return matrix should be
int totalRows = this.TotalRows();
int totalColumns = this.TotalColumns();
Matrix returnMatrix = new Matrix(totalRows, totalColumns);
List<object> rowElementsList = new List<object>();
//"outer" index means an index of the MatricesMatrix
//"inner" index means an index of a Matrix within the Matrices Matrix
//outer row loop
for (int outerRowIndex = 0; outerRowIndex < NumberOfRows; outerRowIndex++)
{
//outer column loop
for (int outerColumnIndex = 0; outerColumnIndex < NumberOfColumns; outerColumnIndex++)
{
Matrix currentMatrix = GetElement(outerRowIndex, outerColumnIndex);
object element = null;
//inner row loop
for (int innerRowIndex = 0; innerRowIndex < currentMatrix.NumberOfRows; innerRowIndex++)
{
//inner column loop
for (int innerColumnIndex = 0; innerColumnIndex < currentMatrix.NumberOfColumns; innerColumnIndex++)
{
element = currentMatrix.GetElement(innerRowIndex, innerColumnIndex);
}
}
returnMatrix.SetElement(outerRowIndex, outerColumnIndex, (double)element);
}
}
return returnMatrix;
}
请注意,我已经通过程序确定了返回矩阵所需的总行数和列数。
以下是一些指南和输出案例:
- 大矩阵的每个元素应该与来自MatricesMatrix内部的Matrix的其他元素相对位置相同。 - 大矩阵内的每个“矩阵”(不再以矩阵形式存在)应该与其在MatricesMatrix内部的其他矩阵相对位置相同(没有重叠,并且在任何空白处留下0)。
案例1
给定此输入:一个2x2的MatricesMatrix,其中[0,0] = 2x2矩阵,[0,1] = 2x3矩阵,[1,0] = 2x2矩阵,[1,1] = 2x3矩阵。即,
输出必须是:
案例2
给定此输入:一个2x2的MatricesMatrix,其中[0,0] = 1x1矩阵,[0,1] = 3x3矩阵,[1,0] = 2x2矩阵,[1,1] = 4x4矩阵。即,
输出应该类似于:
非常感谢您的协助!
更新: 这是Case 1的一个单元测试,应该通过:
[TestMethod]
public void MatricesMatrix_ConvertToMatrixTest()
{
Matrix m1 = new Matrix(2);
Matrix m2 = new Matrix(2, 3);
Matrix m3 = new Matrix(2);
Matrix m4 = new Matrix(2, 3);
double[] m1Row1 = { 1, 1 };
double[] m1Row2 = { 1, 1 };
double[] m2Row1 = { 2, 2, 2 };
double[] m2Row2 = { 2, 2, 2 };
double[] m3Row1 = { 3, 3 };
double[] m3Row2 = { 3, 3 };
double[] m4Row1 = { 4, 4, 4 };
double[] m4Row2 = { 4, 4, 4 };
m1.SetRowOfMatrix(0, m1Row1);
m1.SetRowOfMatrix(1, m1Row2);
m2.SetRowOfMatrix(0, m2Row1);
m2.SetRowOfMatrix(1, m2Row2);
m3.SetRowOfMatrix(0, m3Row1);
m3.SetRowOfMatrix(1, m3Row2);
m4.SetRowOfMatrix(0, m4Row1);
m4.SetRowOfMatrix(1, m4Row2);
MatricesMatrix testMatricesMatrix = new MatricesMatrix(2, 2);
testMatricesMatrix.SetElement(0, 0, m1);
testMatricesMatrix.SetElement(0, 1, m2);
testMatricesMatrix.SetElement(1, 0, m3);
testMatricesMatrix.SetElement(1, 1, m4);
Matrix expectedResult = new Matrix(4, 5);
double[] expectedRow1 = { 1, 1, 2, 2, 2 };
double[] expectedRow2 = { 1, 1, 2, 2, 2 };
double[] expectedRow3 = { 3, 3, 4, 4, 4 };
double[] expectedRow4 = { 3, 3, 4, 4, 4 };
expectedResult.SetRowOfMatrix(0, expectedRow1);
expectedResult.SetRowOfMatrix(1, expectedRow2);
expectedResult.SetRowOfMatrix(2, expectedRow3);
expectedResult.SetRowOfMatrix(3, expectedRow4);
Matrix actualResult = testMatricesMatrix.ConvertToMatrix();
(actualResult == expectedResult).Should().BeTrue();
}