954,515 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How to calculate matrix determinant? n*n or just 5*5.

Hi, everyone. I need to find matrix n*n (or 5*5) determinant. I have a function translated from Pascal, but there's INDEX OUT OF RANGE EXCEPTION. Could somebody help me?
Here's my code:

public static double DET(double[,] a, int n)
        {
            int i, j, k;
            double det = 0;
            for (i = 0; i < n - 1; i++)
            {   
                for (j = i + 1; j < n + 1; j++)
                {
                    det = a[j, i] / a[i, i];
                    for (k = i; k < n; j++)
                        a[j, k] = a[j, k] - det * a[i, k]; // Here's exception
                }
            }
            det = 1;
            for (i = 0; i < n; i++)
                det = det * a[i, i];
                return det;
        }

Thanx for any help.

Dasharnb777
Newbie Poster
15 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
 

Line 10 you increment j instead of k. You'll also probably have an issue with line 7 since you allow j to go to n, which is outside the array bounds.

Momerath
Nearly a Senior Poster
3,386 posts since Aug 2010
Reputation Points: 1,232
Solved Threads: 558
 
Line 10 you increment j instead of k. You'll also probably have an issue with line 7 since you allow j to go to n, which is outside the array bounds.


Oh, exactly! Thanx, but there's still ecxeption.

Dasharnb777
Newbie Poster
15 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
 

Did you change line 7 to j < n rather than j < n + 1 ?

Momerath
Nearly a Senior Poster
3,386 posts since Aug 2010
Reputation Points: 1,232
Solved Threads: 558
 
Did you change line 7 to j < n rather than j < n + 1 ?


Oh, thanx. But it doesn't help...

Dasharnb777
Newbie Poster
15 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
 

Exception still in the same line? Could you post the current version of your code?

Momerath
Nearly a Senior Poster
3,386 posts since Aug 2010
Reputation Points: 1,232
Solved Threads: 558
 
Exception still in the same line? Could you post the current version of your code?


Yes, exception is always in this line.
Current code:

public static double DET(double[,] a, int n)
        {
            int i, j, k;
            double det = 0;
            for (i = 0; i < n - 1; i++)
            {
                for (j = i + 1; j < n; j++)
                {
                    det = a[j, i] / a[i, i];
                    for (k = i; k < n; k++)
                        a[j, k] = a[j, k] - det * a[i, k]; // HERE
                }
            }
            det = 1;
            for (i = 0; i < n; i++)
                det = det * a[i, i];
                return det;
        }
Dasharnb777
Newbie Poster
15 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
 

I've tried to get it to generate an exception in that line and can't do it. What input are you using?

Momerath
Nearly a Senior Poster
3,386 posts since Aug 2010
Reputation Points: 1,232
Solved Threads: 558
 
I've tried to get it to generate an exception in that line and can't do it. What input are you using?


Oh, thanx a lot! I'm so so inconsiderate! I'd wrong input. It works now!

Dasharnb777
Newbie Poster
15 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: