【问题标题】:how to put the last value of a matrix as first value and vice versa如何将矩阵的最后一个值作为第一个值,反之亦然
【发布时间】:2014-11-23 19:51:19
【问题描述】:

伙计们,我一直试图将矩阵的最后一个值作为第一个值,将第一个值作为第二个值,但这是我的结果

生成的矩阵es:

8, 4, 10, 
4, 6, 9, 
3, 9, 7,

倒置矩阵: 7, 9, 3, 7, 9, 3, 7, 9, 3,

这是代码:

public String segmat(int a[][]) {
    String s = "";
    for (int i =0;i<a.length;i++){
    //for (int i = a.length - 1; i >= 0; i--) {
        for (int j = a[0].length - 1; j >= 0; j--) {
            s += a[a.length - 1][j] + ", ";
        }
    }
    return s;
}

【问题讨论】:

  • 您的问题不清楚:什么是“倒置”矩阵,7, 9, 3, 7, 9, 3, 7, 9, 3, 如何表示将最后一个值带入第一个?
  • 您的示例不仅仅是矩阵中第一项和最后一项之间的交换。
  • 根据您的代码,您似乎正在尝试重复矩阵的最后一个原始数据。您为变量s 选择了字符串类型,这意味着您只想打印矩阵而不是任何微积分。我对这段代码的建议是将printline 函数替换为返回s。我什至不知道你的编程语言是什么。请澄清您的问题并将某些语言标记为标签!
  • 好吧,我的问题是,例如,如果我的矩阵是第一行 8、4、10 和第二行 4、6、9 和第三行 3、9、7,我该如何反转这样我就可以在第一行获得 7、9、3,在第二行获得 9、6、4,在最后一行获得 10、4、8 我希望这能让这个想法更清晰

标签: java matrix reverse


【解决方案1】:

这个例子对你有帮助

我对代码进行了注释,所以很容易理解

public class MatrixExample {

    private static int[][] reverse(int[][] matrix){
        int row=matrix.length;
        int column=matrix[0].length; //we have for shure this element, his length is the number of colmn
        int to_return[][] = new int[row][column]; //this is a matrix of the same dimention of the original
        //the indexes that will cicle the row and the column of the new matrix
        int new_row=0; 
        int new_column=0;
        for(int i=row-1;i>-1;i--){ //this will cile the rows from the last to thee first
            for(int j=column-1;j>-1;j--){  //this will cicle the colums from the last to the first
                to_return[new_row][new_column]=matrix[i][j];
                new_column++;
            }
            new_column=0;
            new_row++;
        }
        return to_return;
    }
    public static void main(String[] args) {
        int matrix[][] = {{ 1, 2, 3},
                        {4, 5, 6},
                        {7, 8, 9},
                        {10,11,12}};

        int[][] new_matrix = reverse(matrix);

        for(int i=0;i<new_matrix.length;i++){
            for(int j=0;j<new_matrix[0].length;j++){
                System.out.print(new_matrix[i][j]+" ");
            }
            System.out.println();
        }
    }

}

【讨论】:

  • 谢谢他帮了我很多,我试图只用一个变量来做,最后我意识到我需要更多的帮助。
猜你喜欢
  • 2015-10-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-29
  • 1970-01-01
相关资源
最近更新 更多