Page 1 of 2 12 LastLast
Results 1 to 20 of 27
  1. #1
    lulzim is offline Member
    Join Date
    Feb 2011
    Posts
    44
    Rep Power
    0

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

  2. #2
    lovelesh is offline Senior Member
    Join Date
    Nov 2010
    Location
    Delhi
    Posts
    135
    Blog Entries
    1
    Rep Power
    0

    Smile

    Please use tag for pasting code.

    Its hard to understand your code without code tag.

  3. #3
    yellowledbet is offline Senior Member
    Join Date
    Feb 2011
    Location
    Georgia, USA
    Posts
    122
    Rep Power
    0

    Default

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

    Default

    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. #5
    lulzim is offline Member
    Join Date
    Feb 2011
    Posts
    44
    Rep Power
    0

    Default

    So nobody else have any idea about this how can i do better and at least explain it a bit.

  6. #6
    yellowledbet is offline Senior Member
    Join Date
    Feb 2011
    Location
    Georgia, USA
    Posts
    122
    Rep Power
    0

    Default

    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 04:56 PM.

  7. #7
    lulzim is offline Member
    Join Date
    Feb 2011
    Posts
    44
    Rep Power
    0

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

    Default

    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. #9
    lulzim is offline Member
    Join Date
    Feb 2011
    Posts
    44
    Rep Power
    0

    Default @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. #10
    lulzim is offline Member
    Join Date
    Feb 2011
    Posts
    44
    Rep Power
    0

    Default yellowledbet

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

  11. #11
    yellowledbet is offline Senior Member
    Join Date
    Feb 2011
    Location
    Georgia, USA
    Posts
    122
    Rep Power
    0

    Default

    have you tried? what did you come up with

  12. #12
    lulzim is offline Member
    Join Date
    Feb 2011
    Posts
    44
    Rep Power
    0

    Default

    can u please show me how to try by ur idea, really i cant understand ur idea.

  13. #13
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,732
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by lulzim View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

  14. #14
    lulzim is offline Member
    Join Date
    Feb 2011
    Posts
    44
    Rep Power
    0

    Default

    I think the only way is using nested loop

  15. #15
    lulzim is offline Member
    Join Date
    Feb 2011
    Posts
    44
    Rep Power
    0

    Default

    So this is two for loop not one lol.

  16. #16
    lulzim is offline Member
    Join Date
    Feb 2011
    Posts
    44
    Rep Power
    0

    Default

    Isnt same code like i posted first post????

  17. #17
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,732
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by lulzim View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

  18. #18
    lulzim is offline Member
    Join Date
    Feb 2011
    Posts
    44
    Rep Power
    0

    Default

    Thank u :D for ur solution I really appreciate it.

  19. #19
    yellowledbet is offline Senior Member
    Join Date
    Feb 2011
    Location
    Georgia, USA
    Posts
    122
    Rep Power
    0

    Default

    Is one loop really incorrect?

  20. #20
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,732
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by yellowledbet View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

Page 1 of 2 12 LastLast

Similar Threads

  1. Help with dox matrix printer
    By Albert in forum Advanced Java
    Replies: 7
    Last Post: 09-06-2011, 09:50 AM
  2. Matrix class
    By maz22 in forum New To Java
    Replies: 5
    Last Post: 10-15-2010, 07:53 AM
  3. help in matrix
    By Engineer in forum New To Java
    Replies: 7
    Last Post: 10-06-2010, 02:26 PM
  4. matrix multiply
    By slixtrix in forum New To Java
    Replies: 8
    Last Post: 09-13-2010, 07:50 AM
  5. Help with matrix
    By susan in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 05:37 AM

Posting Permissions

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