简体   繁体   English

将2D阵列添加到3D阵列

[英]Adding a 2D Array to a 3D Array

Im struggling a little with stacking two matrices on top of each other. 我在堆叠两个矩阵的过程中有点挣扎。 I'm using the pyKalman package, which when updated, returns a tuple of matrices. 我正在使用pyKalman软件包,该软件包在更新后会返回一个矩阵元组。 One with an updated estimate ( new_pred a 1 x 2 vector) and the corresponding covariance matrix ( new_cov a 2 x 2 matrix). 一个带有更新的估计值( new_pred 1 x 2向量)和相应的协方差矩阵( new_cov 2 x 2矩阵)。

After the update, I want to stack the returned values to their corresponding outputs, for a recursive smoothing of the data, through these estimates. 更新之后,我想将返回的值堆叠到其相应的输出中,以便通过这些估计来对数据进行递归平滑。

The following is how it is currently implemented. 以下是当前的实现方式。

for meas in onlineObservations:
    (new_pred, new_cov) = kf.filter_update(states_pred[-1], cov_pred[-1], meas)
    states_pred = np.vstack((states_pred, new_pred))
    cov_pred = np.stack((cov_pred, new_cov), axis=0)

Which works really well for the updated estimate (the 1x2 vector), but fails when i try to add new_cov to the array called cov_pred . 这对于更新的估算值(1x2向量)确实非常有效,但是当我尝试将new_cov添加到名为cov_pred的数组时cov_pred For good measure: 好措施:

states_pred.shape = (900,2)
cov_pred.shape = (900, 2, 2)

I've tried changing the axis of "stack" to no avail. 我尝试将“堆栈”的轴更改为无济于事。 It's probably something elementary, but i've been struggling with it for the past hour, and cannot seem to find a "simple" solution. 它可能是基本的东西,但是在过去的一个小时里,我一直在为此苦苦挣扎,而且似乎找不到“简单”的解决方案。

Thanks in advance. 提前致谢。

This should work - 这应该工作-

cov_pred = []
for meas in onlineObservations:
    (new_pred, new_cov) = kf.filter_update(states_pred[-1], cov_pred[-1], meas)
    states_pred = np.vstack((states_pred, new_pred))
    cov_pred.append[new_cov]
cov_pred = np.stack(cov_pred, axis=0)

But since you want to update array which you are using already in code, you should use np.concatenate 但是由于您要更新代码中已在使用的数组,因此应使用np.concatenate

for meas in onlineObservations:
    (new_pred, new_cov) = kf.filter_update(states_pred[-1], cov_pred[-1], meas)
    states_pred = np.vstack((states_pred, new_pred))
    cov_pred = np.concatenate((cov_pred, np.reshape(new_cov, (1,2,2))), axis=0)

I've been able to make it work by converting cov_pred to a list, and then use: 通过将cov_pred转换为列表,然后使用它,我已经能够使其工作:

cov_pred.append(new_cov)

And then re-convert it back again after the for loop. 然后在for循环之后再次将其转换回原来的状态。 But it seems tedious - at least if there's an even better way! 但这似乎很乏味-至少如果有更好的方法!

您可以将代码保存在For循环中(同时循环也可以),并使用“启用自动索引”,就这样。...在Loop的输出中,LabVIEW将根据您的要求创建3D数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM