Results 1 to 4 of 4
Thread: approximate derivative of matrix
- 07-06-2011, 10:35 PM #1
Member
- 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 < 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?
- 07-07-2011, 12:52 AM #2
Member
- 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.
- 07-07-2011, 01:11 AM #3
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Knowing the start point and end point, try doing this mechanically with pen and paper, it may help you understand it better.
- 07-07-2011, 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 calculation-instruction)Last edited by Jodokus; 07-07-2011 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: 09-06-2011, 08:50 AM -
Approximate derivative of matrix with respect to X
By osenna66 in forum New To JavaReplies: 1Last Post: 07-03-2011, 03:48 PM -
help in matrix
By Engineer in forum New To JavaReplies: 7Last Post: 10-06-2010, 01:26 PM -
Take a derivative
By McChill in forum New To JavaReplies: 2Last Post: 05-03-2009, 04:32 AM -
Help with matrix
By susan in forum New To JavaReplies: 1Last Post: 08-07-2007, 04:37 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks