java - 'reshape' 的 MATLAB 函数是否在任何 Java 库中可用?

标签 java matlab reshape

我正在尝试在 Java 中使用 MATLAB 中可用的 reshape 函数的功能。 Java 中是否有可用的 reshape 实现?

最佳答案

我在 sun forums 上找到了这个(稍作修改) .

public class Test {

    public static void main(String[] args) {
        double[][] ori = new double[][] { {1,2,3}, {4,5,6}, {7,8,9}, {10,11,12} };
        double[][] res = reshape(ori,2,6);

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



    }

    public static double[][] reshape(double[][] A, int m, int n) {
        int origM = A.length;
        int origN = A[0].length;
        if(origM*origN != m*n){
            throw new IllegalArgumentException("New matrix must be of same area as matix A");
        }
        double[][] B = new double[m][n];
        double[] A1D = new double[A.length * A[0].length];

        int index = 0;
        for(int i = 0;i<A.length;i++){
            for(int j = 0;j<A[0].length;j++){
                A1D[index++] = A[i][j];
            }
        }

        index = 0;
        for(int i = 0;i<n;i++){
            for(int j = 0;j<m;j++){
                B[j][i] = A1D[index++];
            }

        }
        return B;
    }
}

测试输出为

1.0 2.0 3.0 
4.0 5.0 6.0 
7.0 8.0 9.0 
10.0 11.0 12.0 

1.0 3.0 5.0 7.0 9.0 11.0 
2.0 4.0 6.0 8.0 10.0 12.0 

关于java - 'reshape' 的 MATLAB 函数是否在任何 Java 库中可用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2877310/

相关文章:

matlab - 测试表 Matlab 中是否存在列

r - 如何 'reverse melt' 一个 data.frame?

从R中的数据框中删除重复行

java - 在 Spring Data 中缓存表

java - 使用 Java 中的 JsonPath 解析 JSON 中的数组

java - 递归判断是否可以找到目标数

arrays - 重复数组元素 : Run-length decoding in MATLAB 的副本

image - 在 MATLAB imagesc 函数中显示网格线

r - 长转宽格式: keep row orders and use only part of row values for newly created column names

java - 在关系双方非法使用mappedBy