0

How to find average of each 8 element in a 2D-matrix(5×5) and replace it in element number(9) ?

in java programming (OOP)

in frist code should be like this and For example :

img
(source: 0zz0.com)

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
user3554225
  • 35
  • 1
  • 9
  • 3
    i think you are talking about matrix convolution http://en.wikipedia.org/wiki/Kernel_(image_processing) – kritzikratzi Apr 20 '14 at 16:00
  • 1
    I really wish there was some sort of code (or pseudo code) that went with this. I really like the question and images. Show us what you tried so I can upvote this. |=^] – But I'm Not A Wrapper Class Apr 20 '14 at 16:04
  • @CyberneticTwerkGuruOrc _et al_: Look at `ConvolveOp`, mentioned [here](http://stackoverflow.com/a/14372048/230513). – trashgod Apr 20 '14 at 16:04
  • You need to give us more info. How is your matrix represented? 1D array or 2D array? is it integers or floats or some custom item? – Joshua Kissoon Apr 20 '14 at 16:19
  • I don't know how to add code but this is my code for the main program http://www8.0zz0.com/2014/04/20/16/822478277.png – user3554225 Apr 20 '14 at 16:51

2 Answers2

1

So I just did it for fun, here's my code. It would work for any size matrix.

public class Matrixer
{

    final double[][] matrix, computedMatrix;
    final int rows, cols;

    public Matrixer(int N, int M, final double[][] imatrix)
    {
        rows = N;
        cols = M;
        matrix = imatrix;
        computedMatrix = new double[N][M];
    }

    public void computeAverages()
    {
        for (int i = 1; i < rows - 1; i++)
        {
            for (int j = 1; j < cols - 1; j++)
            {
                computedMatrix[i][j] = cellNeighborsAverage(i, j);
            }
        }
    }

    private double cellNeighborsAverage(int row, int col)
    {
        // Ignore center cell
        double sum = matrix[row - 1][col - 1] + matrix[row - 1][col]
                + matrix[row - 1][col + 1] + matrix[row][col - 1]
                + matrix[row][col + 1] + matrix[row + 1][col - 1]
                + matrix[row + 1][col] + matrix[row + 1][col + 1];
        return sum / 8;
    }

    public void printComputedMatrix()
    {
        for (int i = 0; i < rows; i++)
        {
            for (int j = 0; j < cols; j++)
            {
                System.out.printf("%.2f", computedMatrix[i][j]);
                System.out.print(", ");
            }
            System.out.println();
        }
    }

    public static void main(String[] args)
    {
        final double[][] matrix =
        {
            {1, 2, 3, 4, 5},
            {5, 4, 3, 5, 1},
            {3, 2, 2, 3, 4},
            {2, 3, 4, 5, 3},
            {3, 2, 4, 5, 6},
        };

        Matrixer mx = new Matrixer(5, 5, matrix);
        mx.computeAverages();
        mx.printComputedMatrix();
    }
}

Test Output:

0.00, 0.00, 0.00, 0.00, 0.00, 
0.00, 2.63, 3.13, 3.13, 0.00, 
0.00, 3.25, 3.63, 3.38, 0.00, 
0.00, 2.75, 3.25, 3.88, 0.00, 
0.00, 0.00, 0.00, 0.00, 0.00
Joshua Kissoon
  • 3,269
  • 6
  • 32
  • 58
0

Pseudocode, as this is not really a Java-specific question:

var myMatrix = [
  [ x, x, x, x, x],
  [ x, x, x, x, x],
  [ x, x, x, x, x],
  [ x, x, x, x, x],
  [ x, x, x, x, x] ];

function neighborAverage(row, col) {
  return (( myMatrix[row-1][col-1] +
            myMatrix[row-1][col] +
            myMatrix[row-1][col+1] +
            myMatrix[row][col-1] +
            // skip the center element
            myMatrix[row][col+1] +
            myMatrix[row+1][col-1] +
            myMatrix[row+1][col] +
            myMatrix[row+1][col+1]) / 8));
}

function matrixAverage() {
  return ([
    [ neighborAverage(1, 1), neighborAverage(1, 2), neighborAverage(1, 3) ],
    [ neighborAverage(2, 1), neighborAverage(2, 2), neighborAverage(2, 3) ],
    [ neighborAverage(3, 1), neighborAverage(3, 2), neighborAverage(3, 3) ] ]);
}

Two notes:

  • hardcoded indices in matrixAverage() means this pseudocode will only work with a matrix of size 5x5. But it shouldn't be too hard to generalize that function to work with matrices of any size.

  • this pseudocode calculates all the averages but does not overwrite any values in the original matrix. That might or might not be what you want, as you didn't specify if each new value should be used to calculate subsequent values (and if so, in what order the values should be calculated).

Dan O
  • 6,022
  • 2
  • 32
  • 50