这可能是一个有点愚蠢的问题,我可能也误解了解决这个问题的最佳方法,但我基本上想做的是以下内容:
我想将以下矩阵相乘以得到结果-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
对此的任何帮助都将非常感激。提前谢谢!
答案 0 :(得分:2)
答案 1 :(得分:2)
听起来你正在寻找
double x = c.get(0, 0);
此外,您的矩阵具有不兼容的乘法尺寸。似乎第二个矩阵应该像这样构造:
Matrix b = new Matrix( bArray, bArray.length );
答案 2 :(得分:2)
您只需使用get()方法:
double x = c.get(0,0);
请注意,您将获得IllegalArgumentException,因为您尝试将两个行向量相乘。来自times()
documentation:
java.lang.IllegalArgumentException - Matrix inner dimensions must agree.
您可能希望将第二个数组转换为列向量。