【发布时间】:2014-08-31 22:22:43
【问题描述】:
所以我有一个对象叫做MatricesMatrix,它是矩阵的Matrix(所有内容都存储为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;
}
请注意,我已经以编程方式确定了 returnMatrix 所需的总行数和列数。
这里有一些更多的指导方针和输出案例:
- 大矩阵的每个元素都应相对于来自该元素所在的 MatricesMatrix 内部的矩阵的大矩阵的其他元素处于相同的位置。
- 大矩阵内的每个“矩阵”(不再以矩阵形式)相对于大矩阵内的其他矩阵应位于与矩阵矩阵内相同的位置(没有重叠,并且 0 位于任何空格留空)。
案例 1
给定这个输入:MatricesMatrix(2,2) 和 [0,0] = (2x2 matrix), [0,1] = (2x3 matrix), [1,0] = (2x2 matrix), and [1,1] = (2x3 matrix)。也就是说,
输出必须是:
案例 2
给定这个输入:MatricesMatrix(2,2) 和 [0,0] = (1x1 matrix), [0,1] = (3x3 matrix), [1,0] = (2x2 matrix), and [1,1] = (4x4 matrix)。也就是说,
输出应该是这样的:
任何帮助将不胜感激!
更新: 这是一个应该通过的案例 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();
}
【问题讨论】:
-
如果您将 2x2 矩阵向上移动两个块并将 3x3 矩阵向左移动 1 个块,这是否可以接受?
-
你的意思是把 3x3 移到 RIGHT 1 块吗?是的,我认为这是一种可能性,只要该方法仍然可以处理比我给出的示例更大的矩阵矩阵
-
我不确定解决方案必须通过哪些标准。您展示的是 afaics 不是最小的 2d 矩阵,它可以容纳较小的矩阵而不会重叠。所以一个人可以将它们排列在彼此下方,不是吗?
-
@PeterSchneider 对于案例 2 来说,这可能是一个更好的方法(尽管我仍然不确定如何做到这一点)。我添加了一个更简单的案例(案例 1),其中每列中的矩阵都具有相同的维度,以帮助澄清我所追求的输出
-
你能制定规则吗?编程正在以正式的符号表示规则。没有规则,没有程序。
标签: c# arrays matrix multidimensional-array