location of (i, j, k) that has the maximum difference.

Hello cplusplus!

I am trying to write a function that finds the maximum difference in a 3D array.

So, I have a 3D array, I want to go through all of its elements and calculate the difference between an element and its neighbors.

After I find the difference between each element and its neighbors, I set that difference to new 3D array which has only difference values. After that I look for the highest value. Which will be like this x[i][j][k].

the definition of a neighbor is found here: http://i.imgur.com/INwyS4e.jpg

that's a 2D view, in the 3D view there will be 6 neighbors. top, down, left, right, front, and back.

I came up with this code, I got stuck and I didn't know how to progress with a solution.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
  #include <iostream>
#include <array>
#define NUM_ROW 4
#define NUM_COLUMN 4
#define NUM_DEPTH 3

using namespace std;

/*  (3)	The location (i, j, k)  of the element that has the maximum value difference with its neighboring elements
(1 point). Here (i, j, k) refers to the array indices of the element. The definition of neighboring elements is shown below:

You need to create a 3d array (like D[4][4][3]), and use it to store the maximum neighboring difference with respect to each element.
After the establishment of D array, you need to search this array to find the element with maximum value, and its indices (i, j k), which is the answer to this sub-question.

The key is to set up D matrix. For each D[i][j][k], you need calculate the difference between the center element X[i][j][k] and all the neighboring elements such
as X[i+1][j][k]. And then use the maximum one to be stored in D[i][j][k].

*/

float MaxDiff(float Value[NUM_ROW][NUM_COLUMN][NUM_DEPTH], float maxD[NUM_ROW][NUM_COLUMN][NUM_DEPTH])
{
	float maxDifference;
	float Difference;

	for (int i = 0; i < 4; i++)
		for (int j = 0; j < 4; j++)
			for (int k = 0; k < 3; k++)
			{
	maxD[i][j][k] = Value[i][j][k] - Value[i + 1][j+1][k+1];
			}
	for (int i = 3; i >= 0; i--)   // this for loop print the array's elements
		for (int j = 2; j >= 0; j--)
			for (int k = 2; k >= 0; k--)
			{
		cout << maxD[i][j][k];
			}
		

	cout << endl;

	return 0;
}


int main()
{

	float maxD[4][4][3];

	float Value[NUM_ROW][NUM_COLUMN][NUM_DEPTH] =
	{ { { 1.1, 1.2, 1.3 }, { 1.4, 1.5, 1.6 }, { 1.7, 1.8, 1.9 }, { 2.0, 2.1, 2.2 } },
	{ { 2.1, 2.2, 2.3 }, { 2.4, 2.5, 2.6 }, { 2.7, 2.8, 2.9 }, { 3.0, 3.1, 3.2 } },
	{ { 3.1, 3.2, 3.3 }, { 3.4, 3.5, 3.6 }, { 3.7, 3.8, 3.9 }, { 4.0, 4.1, 4.2 } },
	{ { 4.1, 4.2, 4.3 }, { 4.4, 2.5, 2.6 }, { 2.7, 2.8, 2.9 }, { 3.0, 3.1, 3.2 } } };

	system("pause");
	return 0;

}
Last edited on
Do you have to create a new array containing all the difference values? Why not just store the highest value you've found so far, and replace that variable if you find a new difference that exceeds it? Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
double Diff;
double maxDiff = 0;
int NUM_ROW, NUM_COL, NUM_DEPTH;
double Value[NUM_ROW][NUM_COL][NUM_DEPTH];

for( int i = 0; i < NUM_ROW - 2; i++ )
   {
      for(int j = 0; j < NUM_COL - 2; j++ ) //these are -2 since you actually want to stop at the 2nd to last cell in each dimension
         {
            for(int k = 0; j < NUM_DEPTH - 2; k++ )
               {
                  diff_x = Value[i][j][k] - Value[i+1][j][k];
                  diff_y = Value[i][j][k] - Value[i][j+1][k];
                  diff_z = Value[i][j][k] - Value[i][j][k+1];
                  if( diff_x > maxDiff )
                     {
                        maxDiff = diff_x;
                     }
                  if( diff_y > maxDiff )
                     {
                        maxDiff = diff_y;
                     }
                  if( diff_z > maxDiff )
                     {
                        maxDiff = diff_z;
                     }

               } //close out k
         } //close out j
   } //close out i 
Last edited on
I forgot to mention that it doesn't matter if the difference is negative. The difference should always be positive. In other word, we must use absolute values for the difference so it always come out as positive value.

Also, I tried your idea but it outputted "0".

I don't have to create a new array.

Edit: Sorry! the reason why I have to create new 3D is because I want to find the location of [i][j][k] with the highest difference.

it's possible to do that in the current 3d array though. After we assign the new max difference values we can look for the highest value (highest difference).
Last edited on
Yeah, if you're looking for maximum magnitude of difference you can just put Abs() around the statements.

Even if you need to record the location of the maximum distance, you can do so simply by adding:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
if( diff_x > maxDiff )
                     {
                        maxDiff = diff_x;
                        maxX = i;
                        maxY = j;
                        maxZ = k;
                     }
                  if( diff_y > maxDiff )
                     {
                        maxDiff = diff_y;
                        maxX = i;
                        maxY = j;
                        maxZ = k;
                     }
                  if( diff_z > maxDiff )
                     {
                        maxDiff = diff_z;
                        maxX = i;
                        maxY = j;
                        maxZ = k;
                     }


That said, not sure why it's not working.
Last edited on
BTW, that other code works but in the [code] for( k = 1; k...)]/code] line, there is a j that should be a k.
Edit: nvm, your idea worked. Thank you very much!
Last edited on
Topic archived. No new replies allowed.