Results 1 to 4 of 4
  1. #1
    osenna66 is offline Member
    Join Date
    Jul 2011
    Posts
    18
    Rep Power
    0

    Default approximate derivative of matrix

    The assignment question is: "Approximate the derivative of a matrix with respect to X"

    Using this formula to create a new matrix from the original int[][] m:
    m′[i][j] = (m[i−1][j +1]−m[i−1][j −1])+2∗(m[i][j +1]−m[i][j −1])+(m[i+1][j +1]−m[i+1][j −1]

    Also, boundary cases (ex: m[0][0]) must be treated differently. For example, if the formula says to take m[-1][0], you should use the value for m[0][0] instead.

    This is the code i have written so far:
    public static int[][] approximateDerivativeX(int[][] m) {
    int h = m.length;
    int w = m[0].length;
    int[][] derivativeXArray = new int[h][w];
    for (int i=0; i < h; i++) {
    for (int j=0; j < w; j++) {
    if (i==0 && j==0) {
    derivativeXArray[i][j] = 2*(m[i][j+1]-m[i][j]);
    }
    else if (i==0 && j!=0 && j < w-1) {
    derivativeXArray[i][j] = (m[i][j+1]-m[i][j-1]) + 2*(m[i][j+1]-m[i][j-1]) + (m[i+1][j+1]-m[i+1][j-1]);
    }
    else if (i==0 && j==w-1) {
    derivativeXArray[i][j] = (m[i][j]-m[i][j-1]) + 2*(m[i][j]-m[i][j-1]) + (m[i+1][j]-m[i+1][j-1]);
    }
    else if (i!=0 && i < h-1 && j==0) {
    derivativeXArray[i][j] = (m[i-1][j+1]-m[i-1][j]) + 2*(m[i][j+1]-m[i][j]) + (m[i+1][j+1]-m[i+1][j]);
    }
    else if (i!=0 && i < h-1 && j!=0 && j < w-1) {
    derivativeXArray[i][j] = (m[i-1][j+1]-m[i-1][j-1]) + 2*(m[i][j+1]-m[i][j-1]) + (m[i+1][j+1]-m[i+1][j-1]);
    }
    else if (i!=0 && i < h-1 && j==w-1) {
    derivativeXArray[i][j] = (m[i-1][j]-m[i-1][j-1]) + 2*(m[i][j]-m[i][j-1]) + (m[i+1][j]-m[i+1][j-1]);
    }
    else if (i==h-1 && j==0) {
    derivativeXArray[i][j] = (m[i-1][j+1]-m[i-1][j]) + 2*(m[i][j+1]-m[i][j]) + (m[i][j+1]-m[i][j]);
    }
    else if (i==h-1 && j!=0 && j < w-1) {
    derivativeXArray[i][j] = (m[i-1][j+1]-m[i-1][j-1]) + 2*(m[i][j+1]-m[i][j-1]) + (m[i][j+1]-m[i][j-1]);
    }
    else if (i==h-1 && j==w-1) {
    derivativeXArray[i][j] = (m[i-1][j]-m[i-1][j-1]) + 2*(m[i][j]-m[i][j-1]) + (m[i][j]-m[i][j-1]);
    }
    }
    }

    return derivativeXArray;
    }


    If the original matrix is this:
    7 2 2 5 5
    8 8 1 1 4
    8 4 1 8 0
    0 9 8 6 2
    3 1 5 3 1

    Then the derivative with respect to x should be:
    -10 -17 -1 9 3
    -9 -26 -7 8 -2
    1 -13 -2 -5 -17
    12 11 0 -17 -18
    5 12 1 -14 -8

    But the output I am getting is:
    -10 -22 2 12 3
    -9 -26 -7 8 -2
    1 -13 -2 -5 -17
    12 11 0 -17 -18
    3 14 3 -18 -10

    Any insight in what I am doing wrong?

  2. #2
    osenna66 is offline Member
    Join Date
    Jul 2011
    Posts
    18
    Rep Power
    0

    Default

    i am not a person. i am a slice of pizza. i keep getting pepperoni on my keyboard. please help me.

  3. #3
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Knowing the start point and end point, try doing this mechanically with pen and paper, it may help you understand it better.

  4. #4
    Jodokus's Avatar
    Jodokus is offline Senior Member
    Join Date
    Jan 2011
    Location
    Amsterdam, the Netherlands
    Posts
    230
    Rep Power
    4

    Default

    Hello,
    I checked your calculation, and got the same result (your third matrix).
    I checked your code and found no mistakes, except that the two terms that you threw away in the first line (i and j both zero) don't seem to cancel out.
    The bad news is that when I left them in, only the -10 (upper left, as expected) changed to -15, so my result also is not your desired output.

    Some questions:
    - I have no idea what this "approximateDerivative" is, or is good for. Can you just explain a bit of what you use it for or what it does? (Yes I tried google)
    - What is your source of knowing what the output should be? Is there a link to this source? (Maybe including the calculation-instruction)
    Last edited by Jodokus; 07-07-2011 at 09:54 PM.
    No bug ever had to calculate its fitnessfunction.

Similar Threads

  1. Help with dox matrix printer
    By Albert in forum Advanced Java
    Replies: 7
    Last Post: 09-06-2011, 08:50 AM
  2. Approximate derivative of matrix with respect to X
    By osenna66 in forum New To Java
    Replies: 1
    Last Post: 07-03-2011, 03:48 PM
  3. help in matrix
    By Engineer in forum New To Java
    Replies: 7
    Last Post: 10-06-2010, 01:26 PM
  4. Take a derivative
    By McChill in forum New To Java
    Replies: 2
    Last Post: 05-03-2009, 04:32 AM
  5. Help with matrix
    By susan in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 04:37 AM

Tags for this Thread

Posting Permissions

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