Converting a 2D Array Matrix into an identity matrix c#
<p>I am supposed to create a button that will convert an already existing 2D array matrix into an identity matrix. Obviously you need to make sure the amount of columns and rows are the same in the original matrix in order to make it an identity matrix but I'm a little confused on how to go about doing this.</p>

<p>So far, I have:</p>

<pre><code>        private void btnMakeBIdentity_Click(object sender, EventArgs e)
    {
        double[,] identityMatrixB = new double[matrixBRows, matrixBCols];
        if(matrixBRows == matrixBCols)
        {
            identityMatrixB = IdentityMatrix(matrixBRows);
        }
        matrixB = identityMatrixB;

        matrixToString(matrixB, txtFullMatrixB);
    }
</code></pre>

<p>And the method matrixToString:</p>

<pre><code>        private double[,] IdentityMatrix(int n)
     {
        double[,] result = createMatrix(n, n);
        for (int i = 0; i &lt; n; ++i)
            result[i,i] = 1.0;
        return result;
     }
</code></pre>

<p>In this code: matrixB, matrixBRows, matrixBCols are all global variables of the class. Matrix B was created using:</p>

<pre><code>        private void btnCreateMatrixB_Click(object sender, EventArgs e)
    {
        try
        {
            matrixBRows = Convert.ToInt32(txtMatrixBRows.Text);
            matrixBCols = Convert.ToInt32(txtMatrixBCols.Text);

        }
        catch (Exception ex)
        {
            MessageBox.Show("Please check the textboxes for Matrix B's rows and columns. Be sure you are inputing a valid integer.");
        }

        matrixB = createMatrix(matrixBRows, matrixBCols);
        matrixToString(matrixB, txtFullMatrixB);


    }
</code></pre>

<p>An example of output that is given after Matrix B created would be:</p>

<pre><code>8.3   10   5.2   
0.1   6.3   7.8   
7.6   1.3   1.1   
</code></pre>

<p>after running IdentityMatrix after clicking "Make Matrix B Identity" I get:</p>

<pre><code>1.0   10   5.2   
0.1   1.0   7.8   
7.6   1.3   1.0
</code></pre>

<p>Any help or suggestions would be awesome. Thanks!</p>