如何在java中生成行主列和列主矩阵?

时间:2015-04-19 21:20:30

标签: java matrix

我正在尝试完成一项任务,要求我在Java中创建一个方法,当给定所需的高度和宽度时,创建一个行或列主矩阵。

enter image description here 以下是我到目前为止的情况:

public static int[][] increasingMatrix(int width, int height, boolean format){


        if (format) { // generate row-major matrix
            int[][] array = new int[height][];

            int count = 0;

            for (int i = 0; i < height; i++) {
                array[i] = new int[width];
                for (int j = 0; j < width; j++) {
                    array[i][j] = count;
                    count++;
                }
            }

            return array;

        } else {
            int[][] array = new int[width][];


            int count = 0;

            for (int i = 0; i < width; i++) {
                array[i] = new int [height];
                for (int j = 0; j < height; j++) {
                    array[j][i] = count;
                    count ++;
                }
            }

            return array;
        }

    }

然而,当我去尝试对生成的数组运行测试时,列主要矩阵(据我所知)正在生成错误。行主矩阵似乎正确生成。

你能看到我做错的事吗?我已经盯着这几个小时,但似乎无法获得任何突破。

谢谢!

2 个答案:

答案 0 :(得分:1)

你的代码错了。矩阵中的第一个索引始终是宽度。

请记住:矩阵是一个数组数组。第一个索引是矩阵的宽度,第二个是高度。

试试这个:

if(format) {
    return buildRowMajorMatrix(width, height);
} else {
    return buildColumnMajorMatrix(width, height);
}

buildRowMajorMatrix的样子:

private int[][] buildRowMajorMatrix(int width, int height) {

    int[][] matrix = new int[width][height];
    int cellValue = 0;

    for(int columnIndex = 0 ; columnIndex < width ; columnIndex++) {
        for(int rowIndex = 0 ; rowIndex < height ; rowIndex++, cellValue++) {
            matrix[columnIndex][rowIndex] = cellValue;
        }
    }

    return matrix;
}

buildColumnMajorMatrix看起来像:

private int[][] buildColumnMajorMatrix(int width, int height) {

    int[][] matrix = new int[width][height];
    int cellValue = 0;

    for(int rowIndex = 0 ; rowIndex < height ; rowIndex++) {
        for(int columnIndex = 0 ; columnIndex < width ; columnIndex++, cellValue++) {
            matrix[columnIndex][rowIndex] = cellValue;
        }
    }

    return matrix;
}

答案 1 :(得分:0)

在其他情况下:

else {
        int[][] array = new int[width][];
        int count = 0;

        for (int i = 0; i < width; i++) {
            array[i] = new int [height];
            for (int j = 0; j < height; j++) {
                array[j][i] = count;
                count ++;
            }
        }

        return array;
    }

您正在尝试迭代尚未初始化的数组(矩阵行,j-index)。要实现列主矩阵,您需要首先初始化所有行(或切换到更复杂的算法)。

另外我认为两种格式的矩阵的宽度和高度应该相同。行和列多数只表示我们是否按列或行填充矩阵迭代,但我猜这个任务的措辞留有了解释空间。

我的解决方案:

public static int[][] increasingMatrix(int width, int height, boolean format){
    int[][] array = new int[height][width]; // Java's shortcut generating regular array of arrays for you
    int count = 0;
    if (format) { // generate row-major matrix
        for (int i = 0; i < height; i++) {
            for (int j = 0; j < width; j++) {
                array[i][j] = count;
                count++;
            }
        }
    } else {
        for (int j = 0; j < width; j++)) {
             for (int i = 0; i < height; i++ {
                array[i][j] = count;
                count++;
            }
        }
    }

    return array;
}
相关问题