在 C# 中添加矩阵?
我正在尝试使用一些简单的 for 循环在 C# 中将两个矩阵相加。 我将结果存储在数据网格视图中。 然而,最后一个单元格似乎没有添加。 我已经查看这段代码有一段时间了,但似乎无法弄清楚。 我做错什么了吗?
// Adds two matrices together using arrays.
private void menuItemAdd_Click(object sender, EventArgs e)
{
// Create two 2-D arrays
int[,] matrixOne = new int[dgvMatrixOne.RowCount, dgvMatrixOne.ColumnCount];
int[,] matrixTwo = new int[dgvMatrixTwo.RowCount, dgvMatrixTwo.ColumnCount];
// The rows of the total matrix match the rows of the first matrix.
dgvMatrixTotal.RowCount = dgvMatrixOne.RowCount;
// The columns of the total matrix match the columns of the first matrix.
dgvMatrixTotal.ColumnCount = dgvMatrixOne.ColumnCount;
// Fill matrix one with the data in the data grid matrix one.
for (int i = 0; i < dgvMatrixOne.RowCount; i++)
{
for (int j = 0; j < dgvMatrixOne.ColumnCount; j++)
{
matrixOne[i, j] = Convert.ToInt32(dgvMatrixOne[i, j].Value);
}
}
// Fill matrix two with the data in the data grid matrix two.
for (int i = 0; i < dgvMatrixTwo.RowCount; i++)
{
for (int j = 0; j < dgvMatrixTwo.ColumnCount; j++)
{
matrixTwo[i, j] = Convert.ToInt32(dgvMatrixTwo[i, j].Value);
}
}
// Set the total data grid to matrix one + matrix two.
for (int i = 0; i < dgvMatrixOne.RowCount; i++)
{
for (int j = 0; j < dgvMatrixOne.ColumnCount; j++)
{
dgvMatrixTotal[i, j].Value = matrixOne[i, j] + matrixTwo[i, j];
}
}
}
I'm trying to add two matrices together in C# using some simple for loops. I store the results in a data grid view. However, the last cell does not seem to add. I've been looking at this code for a while now and can't seem to figure it out. Did I do something wrong?
// Adds two matrices together using arrays.
private void menuItemAdd_Click(object sender, EventArgs e)
{
// Create two 2-D arrays
int[,] matrixOne = new int[dgvMatrixOne.RowCount, dgvMatrixOne.ColumnCount];
int[,] matrixTwo = new int[dgvMatrixTwo.RowCount, dgvMatrixTwo.ColumnCount];
// The rows of the total matrix match the rows of the first matrix.
dgvMatrixTotal.RowCount = dgvMatrixOne.RowCount;
// The columns of the total matrix match the columns of the first matrix.
dgvMatrixTotal.ColumnCount = dgvMatrixOne.ColumnCount;
// Fill matrix one with the data in the data grid matrix one.
for (int i = 0; i < dgvMatrixOne.RowCount; i++)
{
for (int j = 0; j < dgvMatrixOne.ColumnCount; j++)
{
matrixOne[i, j] = Convert.ToInt32(dgvMatrixOne[i, j].Value);
}
}
// Fill matrix two with the data in the data grid matrix two.
for (int i = 0; i < dgvMatrixTwo.RowCount; i++)
{
for (int j = 0; j < dgvMatrixTwo.ColumnCount; j++)
{
matrixTwo[i, j] = Convert.ToInt32(dgvMatrixTwo[i, j].Value);
}
}
// Set the total data grid to matrix one + matrix two.
for (int i = 0; i < dgvMatrixOne.RowCount; i++)
{
for (int j = 0; j < dgvMatrixOne.ColumnCount; j++)
{
dgvMatrixTotal[i, j].Value = matrixOne[i, j] + matrixTwo[i, j];
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您确定您的矩阵具有完全相同的大小吗,这两行无论如何都很奇怪,因为您从一个矩阵获取行数,但从另一个矩阵获取列数。
我相信您的错误是 MSDN 声明 Item 属性(用于使用 [] 运算符进行类似数组的访问)是:
但您总是以相反的顺序使用它(行在列之前)。
Are you sure that your matrix have exactly the same size, theses two lines are strange anyway because you take the row count from one matrix but the columncount from another.
I believe that your error is that MSDN state that the Item property (used for array-like access with the [] operator) is :
But you allways use it in the invert order (row before column).
在像 C# 这样的语言中,您实际上不需要担心这些事情。 有一些经过尝试和测试的类库可以为您做这类事情,重要的是,它们经过优化,利用了处理器的 SIMD 指令等。如果该语言具有运算符重载,您只需要声明您的矩阵作为对象,并将它们与简单的结果 = mat_a + mat_b 相加。
In a language like C#, you've no real need to worry about this stuff. There are tried and tested class libraries that do that sort of thing for you, and importantly, they're optimised take advantage of your processors' SIMD instructions etc. If the language has operator overloading, you'll just need to do declare your matrices as objects, and add them together with a simple result = mat_a + mat_b.