tags:

views:

1387

answers:

3

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];
            }
        }
    }
+2  A: 

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).

VirtualBlackFox
Sorry I'm going to fix that it turns out that is how you determine the size of a matrix when multiplying matrices
Lucas McCoy
Also, remember that .RowCount will be off by one when .AllowUserToAddRows == true
Jim H.
I already had that set to false as it was automatically adding rows and creating quite a predicament.
Lucas McCoy
+1  A: 

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.

Lee B
I'm sorry for not specifying this in the question but I'm trying to reinvent the wheel to learn more about wheels. I know that there are free libraries out there but I thought this would be a good hobby project.
Lucas McCoy
+2  A: 

I'm not really answering you question as to what is wrong with your code... But I know how your can fix it. Just skip making your own Matrix math and use a 3rd party implementation! Go ahead, be lazy. ;)

I recommend the open source project Math.NET's Iridium Package. It provides most of the matrix transformations you will ever need.

JohannesH
That's the one I'm going to end up using.
Lucas McCoy
Yay! Lazy wins yet again. ;)
JohannesH