【问题标题】:Please help me explain the Java mechanism for transpose a matrix (code is attached)请帮我解释转置矩阵的Java机制(代码附后)
【发布时间】:2016-03-07 05:05:12
【问题描述】:

我是 Java 新手, 目前,我正在练习一些 Java 代码。所以我正在尝试自己构建一个 Matrix 类。但是,我参考了 jama (http://math.nist.gov/javanumerics/jama/doc/Jama/Matrix.html) 的代码。 但我觉得这很奇怪。这是Matrix类的结构,Jama在后面定义。

谁能帮我解释一下为什么transpose()返回X(在我看来,C数组是X的转置元素,X的元素是相同的顺序。但是为什么jama返回X,以及C数组的作用是什么?在这个程序中?)。 非常感谢。

public class Matrix
{

    private double[][] A;// 2-D array to hold matrix element
    private m,n ; // number of column and row.
    // Some constructors but I would like to omit

    //public methods:
     // I don't understand this:

   public Matrix transpose () {
      Matrix X = new Matrix(n,m);
      double[][] C = X.getArray();
      for (int i = 0; i < m; i++) {
         for (int j = 0; j < n; j++) {
            C[j][i] = A[i][j];
         }
      }
      return X; // While it returns X? seem that X does not transpose but C.
                // it seems there is no connection between X and C. what  is the role of C here?
   }


   public double[][] getArray () {
      return A;
   }       

}

【问题讨论】:

标签: java matrix


【解决方案1】:

XC 之间存在连接。当您调用getArray() 时,它会返回A 本身,而不是A 的副本。

所以在transpose() 方法的上下文中,CX.A 相同。

你可以阅读java中引用变量的行为

【讨论】:

  • 是的,我喜欢这个词(“X 和 C 之间存在联系。当您调用 getArray() 时,它返回 A 本身,而不是 A 的副本。”)。如何单击按钮接受答案?我找不到那个按钮。非常感谢你们。
【解决方案2】:

C 只是一个引用与X.A 相同的二维数组的变量。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-18
    • 2016-11-01
    • 2013-10-07
    相关资源
    最近更新 更多