使用java函数转置double [] []矩阵? [英] transpose double[][] matrix with a java function?

查看:123
本文介绍了使用java函数转置double [] []矩阵?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人可以使用以下形式转换Matrix中的Matrix:

Do anybody have a function with which I can transpose a Matrix in Java which has the following form:

double[][]

我的功能如下:

public static double[][] transposeMatrix(double [][] m){
    for (int i = 0; i < m.length; i++) {
        for (int j = i+1; j < m[0].length; j++) {
            double temp = m[i][j];
            m[i][j] = m[j][i];
            m[j][i] = temp;
        }
    }

    return m;
}

但在某处错了。

推荐答案

    public static double[][] transposeMatrix(double [][] m){
        double[][] temp = new double[m[0].length][m.length];
        for (int i = 0; i < m.length; i++)
            for (int j = 0; j < m[0].length; j++)
                temp[j][i] = m[i][j];
        return temp;
    }

这篇关于使用java函数转置double [] []矩阵?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆