【问题标题】:How to multiply two 1D matrices using JAMA?如何使用 JAMA 将两个一维矩阵相乘?
【发布时间】:2011-08-31 16:19:35
【问题描述】:

这可能是一个有点愚蠢的问题,我也可能误解了解决这个问题的最佳方法,但我基本上想要做的是:

我想将以下矩阵相乘得到-0.8。但是,理想情况下,我希望使用 JAMA 函数来执行此操作。到目前为止,我有以下内容,我想我快到了,这只是我坚持的最后一步..

// Create the two arrays (in reality I won't be creating these, the two 1D matrices
// will be the result of other calculations - I have just created them for this example)
double[] aArray = [0.2, -0.2];
double[] bArray = [0, 4];

// Create matrices out of the arrays
Matrix a = new Matrix( aArray, 1 );
Matrix b = new Matrix( bArray, 1 );

// Multiply matrix a by matrix b to get matrix c
Matrix c = a.times(b);

// Turn matrix c into a double
double x = // ... this is where I'm stuck

对此的任何帮助将不胜感激。提前致谢!

【问题讨论】:

  • IIRC,矩阵的乘积需要第一个矩阵的列数等于第二个矩阵的行数......我不是 JAMA 专家,但它像您的矩阵具有相同的维度(并且不是方阵)

标签: java matrix matrix-multiplication jama


【解决方案1】:

你的意思是使用 get 吗?

double x = c.get(0, 0);

http://math.nist.gov/javanumerics/jama/doc/

【讨论】:

    【解决方案2】:

    听起来你正在寻找

    double x = c.get(0, 0);
    

    此外,您的矩阵具有不兼容的乘法维度。看起来第二个矩阵应该这样构造:

    Matrix b = new Matrix( bArray, bArray.length );
    

    【讨论】:

      【解决方案3】:

      您可以简单地使用get() 方法:

      double x = c.get(0,0);
      

      请注意,您将收到 IllegalArgumentException,因为您尝试将两个行向量相乘。来自times() documentation:

      java.lang.IllegalArgumentException - Matrix inner dimensions must agree.
      

      您可能希望将第二个数组变成列向量。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-04-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-12-27
        • 1970-01-01
        相关资源
        最近更新 更多