Search code examples
javamatrixnullconv-neural-networkconvolution

Why does creating a blank matrix return null instead of 0.0?


I have a Java method fill0s() that inputs a smaller matrix into a larger matrix, supposedly filled with 0s:

public class PoolingFunctions {

    // Fill needed spaces with 0
    public static Double[][] fill0s(Double[][] image, int filter_size){
        int columns_remaining = filter_size - (image[0].length%filter_size);
        int rows_remaining = filter_size - (image.length%filter_size);
        // CREATE A MATRIX: HERE LIES THE PROBLEM: //////////////////////////////
        Double [][] blank_matrix = new Double [(image.length)+columns_remaining][(image[0].length)+rows_remaining];
            for (int i = 0; i<image.length; i++) {
                for (int j = 0; j<image[i].length; j++) {
                    blank_matrix[i][j] = image[i][j];
                }
            }

        return blank_matrix;
    }
}

And after calling this method:

public class MainPooling{
    public static void main(String[] args) {
        Double[][] image =  {   {1.0,1.0,1.0,1.0},
                                {1.0,1.0,1.0,1.0},
                                {1.0,1.0,1.0,1.0},
                                {1.0,1.0,1.0,1.0}
                            };

        Double[][] formatted_image;
        formatted_image = PoolingFunctions.fill0s(image,3);
        TestMatricesJava.print2dMatrix(formatted_image);
    }
}

I get the following result:

1.0 1.0 1.0 1.0 null null 
1.0 1.0 1.0 1.0 null null 
1.0 1.0 1.0 1.0 null null 
1.0 1.0 1.0 1.0 null null 
null null null null null null 
null null null null null null 

My question is: Why does the function return a null instead of a 0.0?


Solution

  • Double is a wrapper class whose value will be null if not initialized. Use primitive type double and it will give you a correct result.