Results 1 to 4 of 4
Thread: approximate derivative of matrix
 07062011, 10:35 PM #1Member
 Join Date
 Jul 2011
 Posts
 18
 Rep Power
 0
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 < w1) {
derivativeXArray[i][j] = (m[i][j+1]m[i][j1]) + 2*(m[i][j+1]m[i][j1]) + (m[i+1][j+1]m[i+1][j1]);
}
else if (i==0 && j==w1) {
derivativeXArray[i][j] = (m[i][j]m[i][j1]) + 2*(m[i][j]m[i][j1]) + (m[i+1][j]m[i+1][j1]);
}
else if (i!=0 && i < h1 && j==0) {
derivativeXArray[i][j] = (m[i1][j+1]m[i1][j]) + 2*(m[i][j+1]m[i][j]) + (m[i+1][j+1]m[i+1][j]);
}
else if (i!=0 && i < h1 && j!=0 && j < w1) {
derivativeXArray[i][j] = (m[i1][j+1]m[i1][j1]) + 2*(m[i][j+1]m[i][j1]) + (m[i+1][j+1]m[i+1][j1]);
}
else if (i!=0 && i < h1 && j==w1) {
derivativeXArray[i][j] = (m[i1][j]m[i1][j1]) + 2*(m[i][j]m[i][j1]) + (m[i+1][j]m[i+1][j1]);
}
else if (i==h1 && j==0) {
derivativeXArray[i][j] = (m[i1][j+1]m[i1][j]) + 2*(m[i][j+1]m[i][j]) + (m[i][j+1]m[i][j]);
}
else if (i==h1 && j!=0 && j < w1) {
derivativeXArray[i][j] = (m[i1][j+1]m[i1][j1]) + 2*(m[i][j+1]m[i][j1]) + (m[i][j+1]m[i][j1]);
}
else if (i==h1 && j==w1) {
derivativeXArray[i][j] = (m[i1][j]m[i1][j1]) + 2*(m[i][j]m[i][j1]) + (m[i][j]m[i][j1]);
}
}
}
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?
 07072011, 12:52 AM #2Member
 Join Date
 Jul 2011
 Posts
 18
 Rep Power
 0
i am not a person. i am a slice of pizza. i keep getting pepperoni on my keyboard. please help me.
 07072011, 01:11 AM #3
 Join Date
 Jan 2011
 Location
 Richmond, Virginia
 Posts
 3,069
 Blog Entries
 3
 Rep Power
 11
Knowing the start point and end point, try doing this mechanically with pen and paper, it may help you understand it better.
 07072011, 04:35 AM #4
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 calculationinstruction)Last edited by Jodokus; 07072011 at 09:54 PM.
No bug ever had to calculate its fitnessfunction.
Similar Threads

Help with dox matrix printer
By Albert in forum Advanced JavaReplies: 7Last Post: 09062011, 08:50 AM 
Approximate derivative of matrix with respect to X
By osenna66 in forum New To JavaReplies: 1Last Post: 07032011, 03:48 PM 
help in matrix
By Engineer in forum New To JavaReplies: 7Last Post: 10062010, 01:26 PM 
Take a derivative
By McChill in forum New To JavaReplies: 2Last Post: 05032009, 04:32 AM 
Help with matrix
By susan in forum New To JavaReplies: 1Last Post: 08072007, 04:37 AM
Bookmarks