1. Member
Join Date
Feb 2011
Posts
44
Rep Power
0

## 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;
}
>

2. Senior Member
Join Date
Nov 2010
Location
Delhi
Posts
135
Blog Entries
1
Rep Power
0
Please use tag for pasting code.

Its hard to understand your code without code tag.

3. Senior Member
Join Date
Feb 2011
Location
Georgia, USA
Posts
122
Rep Power
0
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]

4. Member
Join Date
Mar 2011
Posts
7
Rep Power
0
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

5. Member
Join Date
Feb 2011
Posts
44
Rep Power
0
So nobody else have any idea about this how can i do better and at least explain it a bit.

6. Senior Member
Join Date
Feb 2011
Location
Georgia, USA
Posts
122
Rep Power
0
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
Last edited by yellowledbet; 03-29-2011 at 03:56 PM.

7. Member
Join Date
Feb 2011
Posts
44
Rep Power
0

## 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 ?

8. Senior Member
Join Date
Feb 2011
Location
Georgia, USA
Posts
122
Rep Power
0
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?

9. Member
Join Date
Feb 2011
Posts
44
Rep Power
0

## @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

10. Member
Join Date
Feb 2011
Posts
44
Rep Power
0

## yellowledbet

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

11. Senior Member
Join Date
Feb 2011
Location
Georgia, USA
Posts
122
Rep Power
0
have you tried? what did you come up with

12. Member
Join Date
Feb 2011
Posts
44
Rep Power
0
can u please show me how to try by ur idea, really i cant understand ur idea.

13. 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:

Java 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

14. Member
Join Date
Feb 2011
Posts
44
Rep Power
0
I think the only way is using nested loop

15. Member
Join Date
Feb 2011
Posts
44
Rep Power
0
So this is two for loop not one lol.

16. Member
Join Date
Feb 2011
Posts
44
Rep Power
0
Isnt same code like i posted first post????

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

18. Member
Join Date
Feb 2011
Posts
44
Rep Power
0
Thank u :D for ur solution I really appreciate it.

19. Senior Member
Join Date
Feb 2011
Location
Georgia, USA
Posts
122
Rep Power
0
Is one loop really incorrect?

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

Page 1 of 2 12 Last

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•