Results 1 to 20 of 27
Thread: matrix isDiagonaL?
 03282011, 03:10 PM #1Member
 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;
}
>
Can u please explain me a bit about this where i did mistake or if anybody know any better solution please show me.
 03282011, 04:19 PM #2Senior 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.
 03282011, 05:54 PM #3Senior 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]
 03282011, 07:24 PM #4Member
 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 Autogenerated 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
001
010
100
or matrix with all primitiv element
 03292011, 02:59 PM #5Member
 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.
 03292011, 03:13 PM #6Senior 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 trueLast edited by yellowledbet; 03292011 at 03:56 PM.
 03292011, 04:00 PM #7Member
 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 ?
 03292011, 04:24 PM #8Senior 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?
 03292011, 04:24 PM #9Member
 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
 03292011, 04:26 PM #10Member
 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 ??
 03292011, 04:30 PM #11Senior Member
 Join Date
 Feb 2011
 Location
 Georgia, USA
 Posts
 122
 Rep Power
 0
have you tried? what did you come up with
 03292011, 04:34 PM #12Member
 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.
 03292011, 04:35 PM #13
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,658
 Blog Entries
 7
 Rep Power
 21
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;
Joscenosillicaphobia: the fear for an empty beer glass
 03292011, 04:35 PM #14Member
 Join Date
 Feb 2011
 Posts
 44
 Rep Power
 0
I think the only way is using nested loop
 03292011, 04:36 PM #15Member
 Join Date
 Feb 2011
 Posts
 44
 Rep Power
 0
So this is two for loop not one lol.
 03292011, 04:37 PM #16Member
 Join Date
 Feb 2011
 Posts
 44
 Rep Power
 0
Isnt same code like i posted first post????
 03292011, 04:38 PM #17
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,658
 Blog Entries
 7
 Rep Power
 21
 03292011, 04:40 PM #18Member
 Join Date
 Feb 2011
 Posts
 44
 Rep Power
 0
Thank u :D for ur solution I really appreciate it.
 03292011, 04:44 PM #19Senior Member
 Join Date
 Feb 2011
 Location
 Georgia, USA
 Posts
 122
 Rep Power
 0
Is one loop really incorrect?
 03292011, 04:47 PM #20
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,658
 Blog Entries
 7
 Rep Power
 21
Yep, if any of the numbers not on the main diagonal is nonzero the matrix is not a diagonal matrix; you have to check all those elements in each row and each column. Without trickerydickery you need two loops, one for the rows and one for the columns of the matrix.
kind regards,
Joscenosillicaphobia: the fear for an empty beer glass
Similar Threads

Help with dox matrix printer
By Albert in forum Advanced JavaReplies: 7Last Post: 09062011, 08:50 AM 
Matrix class
By maz22 in forum New To JavaReplies: 5Last Post: 10152010, 06:53 AM 
help in matrix
By Engineer in forum New To JavaReplies: 7Last Post: 10062010, 01:26 PM 
matrix multiply
By slixtrix in forum New To JavaReplies: 8Last Post: 09132010, 06:50 AM 
Help with matrix
By susan in forum New To JavaReplies: 1Last Post: 08072007, 04:37 AM
Bookmarks