# matrix isDiagonaL?

Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last
• 03-28-2011, 03:10 PM
lulzim
matrix isDiagonaL?
Hi guys im facing with an problem with Matrixes.
Actually what im facing is how to check whether the matrix is diagonal or not

ex: 100
010
001
this is an diagonal matrix.

So my code is like below:

<public boolean isDiagonal() {

int i,j;

for(i=0;i<rows;i++)
{
for(j=0;j<columns;j++)
{

if( (this.checkNull((E) this.vals[i][j]) == false) && (i!=j)){
return false;
}

}

}
return true;
}

private boolean checkNull(E elem){

boolean z = false;
if(( elem.toString().equals("0") ) || (elem.toString().equals("false") ))
{
z = true;
}
return z;
}
>

• 03-28-2011, 04:19 PM
lovelesh
Please use tag for pasting code.

Its hard to understand your code without code tag.
• 03-28-2011, 05:54 PM
yellowledbet
I think below are the indexes you are saying you need to compare. If so you only need one loop. Before I entered the loop I would check that there are an equal number of rows and columns.

foo[0][0]
foo[1][1]
foo[2][2]
foo[3][3]
...
foo[n][n]
• 03-28-2011, 07:24 PM
BOJANM
public class MatrixDiagonal {
static int[][] matrix = { { 1, 0, 0 }, { 0, 1, 0 }, { 0, 0, 1 }, };
static int positionx = 0, positiony = 0;
static int temp;
static int count = 0;

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
for (int i = 0; i <= matrix.length - 1; i++) {
for (int j = 0; j <= matrix.length - 1; j++) {
temp = matrix[i][j];
if (temp == 1) {
if (positionx == i && positiony == j) {
positionx++;
positiony++;
count++;
}

}

}
}
if (count == matrix.length)
System.out.println(" Matrix have diagonal");
else
System.out.println("matrix dont have");
}

}
try to think it for revers diagonal
0-0-1
0-1-0
1-0-0

or matrix with all primitiv element
• 03-29-2011, 02:59 PM
lulzim
So nobody else have any idea about this how can i do better and at least explain it a bit.
• 03-29-2011, 03:13 PM
yellowledbet
1 0 0
0 1 0
0 0 1

what are the indexes of the elements in the diagnol? (I gave you the answer in the earlier post)
How would you loop through this structure to access those elements. (Again a single loop should suffice)

You need to make sure that each diagnol element passes some test ( in this case it looks like it needs to be equal to 1 or they all have to be the same value -- I am not sure which one based on your post)

If ANY of the elements fail the test then the method needs to return false
If ALL of the elements pass the test then the method needs to return true
• 03-29-2011, 04:00 PM
lulzim
sorry
Sorry I was wrong when i gave the example of isDiagonal matrix:
200
040
001
This is also diagonal matrix its not necessary only 111.
Now i think u got the right question ?
• 03-29-2011, 04:24 PM
yellowledbet
This is still not clear to me.
By your assignment specifications what should the isDiagonal method return for the two structures below?

is this a diagonal (equal values in all diagonal elements)?
200
020
002

is this a diagonal (all diagonal elements are non zero)?
100
020
003

Have you figured out how to write a loop that access the index of each diagonal element?
• 03-29-2011, 04:24 PM
lulzim
@bojanm
Sorry man but ur code is not correct to determine either the matrix is diagonal or not for ex:
111
111
111

according to ur code it says is diagonal (in fact this is not diagonal matrix) but i know that ur solution is very well with few mistakes.

to be diagonal need to be like this:
100
030
004
• 03-29-2011, 04:26 PM
lulzim
yellowledbet
YEAH both of them are diagonal so how can u make with one loop?? like u said ??
• 03-29-2011, 04:30 PM
yellowledbet
have you tried? what did you come up with
• 03-29-2011, 04:34 PM
lulzim
can u please show me how to try by ur idea, really i cant understand ur idea.
• 03-29-2011, 04:35 PM
JosAH
Quote:

Originally Posted by lulzim
YEAH both of them are diagonal so how can u make with one loop?? like u said ??

That is incorrect; for a matrix to be diagonal, all the elements at position (i,j) for i != j need to be zero, so you need to check the entire matrix except the elements on the diagonal:

Code:

```for (int i= 0; i < matrix.length; i++)   for (int j= 0; j < matrix.length; j++) // assume a square matrix       if (i != j && matrix[i][j] != 0) return false; return true;```
kind regards,

Jos
• 03-29-2011, 04:35 PM
lulzim
I think the only way is using nested loop
• 03-29-2011, 04:36 PM
lulzim
So this is two for loop not one lol.
• 03-29-2011, 04:37 PM
lulzim
Isnt same code like i posted first post????
• 03-29-2011, 04:38 PM
JosAH
Quote:

Originally Posted by lulzim
So this is two for loop not one lol.

I know, I said that the one loop solution is incorrect and I spoonfed you with the correct, two loops, solution.

kind regards,

Jos
• 03-29-2011, 04:40 PM
lulzim
Thank u :D for ur solution I really appreciate it.
• 03-29-2011, 04:44 PM
yellowledbet
Is one loop really incorrect?
• 03-29-2011, 04:47 PM
JosAH
Quote:

Originally Posted by yellowledbet
Is one loop really incorrect?

Yep, if any of the numbers not on the main diagonal is non-zero the matrix is not a diagonal matrix; you have to check all those elements in each row and each column. Without trickery-dickery you need two loops, one for the rows and one for the columns of the matrix.

kind regards,

Jos
Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last