Advertisement
Guest User

Nearest neighbour

a guest
May 11th, 2020
394
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.37 KB | None | 0 0
  1. // Nearest neighbour algorithm. Expands an 8x8 matrix into a 15x15 matrix.
  2.  
  3. float n_map[8][8];    // Read the data from the sensor into this matrix.
  4. float e_map[15][15];  // Holds the resulting matrix data.
  5. int size = 8;
  6. int e_size = 15;
  7.  
  8. // In the loop
  9.   int r=0;
  10.   int c=0;
  11.   int i=0;
  12.   int j=0;
  13.  
  14.   // Load the 8x8 data into the target matrix.
  15.   // Data from even numbered rows are loaded into positions 0,2,4,6,8,10,12,14
  16.   // Odd numbered rows remain empty.
  17.   for (r=0; r < size; r++){
  18.       for(c=0;  c < size; c++){
  19.         e_map[(r+j)][(c+i)] = (float)n_map[r][(c)];
  20.         ++i;
  21.       }
  22.     i=0;
  23.     j++;
  24.   }
  25.  
  26.   // Fill in the blanks as the average of the nearest neighbours.
  27.   // e_map will hold the expanded matrix.  matrix holds the serialized data as a string.
  28.  
  29.     for (r=0; r<e_size; r++){
  30.       for(c=0;  c < e_size; c++){
  31.            if ((c % 2 != 0) && (c < (e_size-1))) {
  32.                e_map[r][c] = (float)((e_map[r][c-1] + e_map[r][c+1]) / 2);
  33.             }
  34.             if ((r % 2 != 0) && (r < (e_size-1))) {
  35.                e_map[r][c] = (float)((e_map[r-1][c] + e_map[r+1][c] ) / 2);
  36.             }
  37.         // This section is only used to serialize the data into a string.
  38.            if ((c==0) && (r == 0)){
  39.              matrix = e_map[r][c];
  40.            } else {
  41.               matrix = matrix + ", " + e_map[r][c];
  42.            }
  43.       }
  44.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement