Matlab中向量与从t到t+1的矩阵对角线的乘法
对编程仍然很陌生...
我在时间 t、t+1、t+2 等处有 9x1 个向量。
[10 10 10 10 10 10 10 10 10]'
和矩阵。每个矩阵都是 9x9,并且在时间 1、t+1、t+2 等 =
1 0 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0
0 0 0 1 0 0 0 0 0
0 0 0 0 1 0 0 0 0
0 0 0 0 0 1 0 0 0
0 0 0 0 0 0 1 0 0
0 0 0 0 0 0 0 1 0
0 0 0 0 0 0 0 0 1
它们是 3d 矩阵,我想将来将它们变成 4d 矩阵。
我想将向量(:,:,t) 与时间t 处的矩阵对角线相乘并输出向量(:,:,t+1)。
简而言之...
向量 t 乘以对角矩阵 t = 向量 t+1
向量 t+1 乘以对角矩阵 t+1 = 向量 t+2
向量 t+2 乘以对角矩阵 t+2 = 向量 t+3 ... 等等。
对角线数字在每个时间步长中都会发生变化,但为了简单起见,我们暂时将它们全部保留为 1。
我尝试过使用 diag,但它指出我必须使用 2D 输入,因此仅当我忽略 t 时才有效。
为你们的帮助干杯——它帮助我学到了很多东西。任何提示或解决方案将不胜感激。我知道你们知道最简单、最有效的解决方案。
Still very new to programming...
I have 9x1 Vectors at time t, t+1, t+2 etc.
[10 10 10 10 10 10 10 10 10]'
and matrices. Each matrix is 9x9 and also at time 1, t+1, t+2 etc. =
1 0 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0
0 0 0 1 0 0 0 0 0
0 0 0 0 1 0 0 0 0
0 0 0 0 0 1 0 0 0
0 0 0 0 0 0 1 0 0
0 0 0 0 0 0 0 1 0
0 0 0 0 0 0 0 0 1
They are 3d matrices and I want to make them 4d in the future.
I want to multiply vector(:,:,t) with the diagonal of matrix at time t and output vector(:,:,t+1).
So in short...
vector t multiplied by diag matrix t = vector t+1
vector t+1 multiplied by diag matrix t+1 = vector t+2
vector t+2 multiplied by diag matrix t+2 = vector t+3 ... and so on.
the diagonal numbers change in each time step but for simplicity sake, let's keep them all at 1 for the moment.
I've tried using diag but it states I have to use a 2D input so only works when I ignore t.
Cheers for your help guys - it's helping me learn a lot. Any hints or solutions will be much appreciated. I know you guys know the simplest and most efficient solutions.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于每个步骤的结果取决于先前的迭代,因此无法对其进行向量化。所以我会选择 @JohnColby 的解决方案。
无论如何,这里是一个如何以矢量化方式提取 3D 矩阵对角线的示例:
结果的每一列对应于每个切片的对角线元素(不一定是方阵):
Since the result of each step depends on the previous iteration, it cannot be vectorized. So I would go with @JohnColby's solution.
For what it's worth, here is an example how you would extract the diagonals of a 3D matrix in a vectorized way:
Each column of the result corresponds to the diagonal elements from each slice (doesn't have to be square matrix):
给你:
现在所有
t
都应该是 1。Here you go:
Now all of
t
should be 1.