为什么 Jama 矩阵的内部维度在第一次迭代中一致,但后来却不一致? [英] Why does the Jama matrix inner dimension agree in the first iteration, but later it does not?

查看:22
本文介绍了为什么 Jama 矩阵的内部维度在第一次迭代中一致,但后来却不一致?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的代码中定义了以下 Jama 矩阵:

Following Jama Matrices are defined in my code:

P: 3*3 Matrix
I: 3*3 identity Matrix
K: 3*2 Matrix
H: 2*3 Matrix
Q: 3*3 Matrix

以下是我的代码片段:

private Matrix getP() {
        P= (I.minus(K.times(H))).times(Q);
        Log.d("csv", "P is calculated");
        return P;
    }

在运行代码时,在第一次迭代时它起作用,即P is计算被打印在Logcat上.但是,它只发生一次并且应用程序被停止.以下是错误:

While running the code, at first iteration it works, i.e, P is calculated is printed at the Logcat. However, it happens only once and the application gets stopped. Following is the error:

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

如果 Matrix 内部维度是错误,它为什么会在第一次迭代中运行?我在 this 链接.但是,我想不出解决方案.手动检查方程时,矩阵维数匹配.我的方法有什么问题吗??

If the Matrix inner dimension was the error, how come it runs for the first iteration? I obtained some information about the inner dimension at this link. However, I could not figure out the solution. When the equation is manually checked, the matrix dimension matches. Anything wrong with my approach??

谢谢.

推荐答案

你介意展示你是如何调用 getP 的吗?无论我点击 fab 按钮多少次,以下内容都有效.

Do you mind showing how you are calling getP? The following works no matter how many times I click on the fab button.

class MainActivity : AppCompatActivity() {

    val I = Matrix.identity(3,3)
    val K = Matrix(3,2,5.0)
    val H = Matrix(2,3,7.0)
    val Q = Matrix(3,3,8.0)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        fab.setOnClickListener { view ->
            getP()
        }
    }

    private fun getP():Matrix{
        val P = (I.minus(K.times(H))).times(Q)
        Log.d("MainActivity","P is calculated")
        return P
    }
}

getP 返回时,您将结果存储在哪里?您可能会覆盖其中一个矩阵吗?

When getP returns where are you storing the results? Are you possibly overwriting one of the matrices?

更新

如果您的情况是将变量设为 final 不适合您,那么您可以记录每个矩阵的维度,然后调试发生变化的维度.

If your situation is such that making the variables final is not an option for you, then you can log each matrix's dimension and then debug the one that changes.

private fun getP():Matrix{
    Log.d(TAG,"I dimension: ${I.rowDimension} x ${I.columnDimension}")
    Log.d(TAG,"K dimension: ${K.rowDimension} x ${K.columnDimension}")
    Log.d(TAG,"H dimension: ${H.rowDimension} x ${H.columnDimension}")
    Log.d(TAG,"Q dimension: ${Q.rowDimension} x ${Q.columnDimension}")

    val P = (I.minus(K.times(H))).times(Q)
    Log.d(TAG,"P is calculated")
    return P
}

这篇关于为什么 Jama 矩阵的内部维度在第一次迭代中一致,但后来却不一致?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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