在 C# 中添加矩阵?

发布于 2024-07-13 23:25:01 字数 1704 浏览 3 评论 0原文

我正在尝试使用一些简单的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我一直都在从未离去 2024-07-20 23:25:02

您确定您的矩阵具有完全相同的大小吗,这两行无论如何都很奇怪,因为您从一个矩阵获取行数,但从另一个矩阵获取列数。

dgvMatrixTotal.RowCount = dgvMatrixOne.RowCount;
dgvMatrixTotal.ColumnCount = dgvMatrixTwo.ColumnCount;

我相信您的错误是 MSDN 声明 Item 属性(用于使用 [] 运算符进行类似数组的访问)是:

public DataGridViewCell this [
    int columnIndex,
    int rowIndex
] { get; set; }

但您总是以相反的顺序使用它(行在列之前)。

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.

dgvMatrixTotal.RowCount = dgvMatrixOne.RowCount;
dgvMatrixTotal.ColumnCount = dgvMatrixTwo.ColumnCount;

I believe that your error is that MSDN state that the Item property (used for array-like access with the [] operator) is :

public DataGridViewCell this [
    int columnIndex,
    int rowIndex
] { get; set; }

But you allways use it in the invert order (row before column).

梦里梦着梦中梦 2024-07-20 23:25:02

在像 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文