Matlab中向量与从t到t+1的矩阵对角线的乘法

发布于 2024-12-11 08:56:19 字数 1002 浏览 4 评论 0原文

对编程仍然很陌生...

我在时间 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

薄暮涼年 2024-12-18 08:56:19

由于每个步骤的结果取决于先前的迭代,因此无法对其进行向量化。所以我会选择 @JohnColby 的解决方案。

无论如何,这里是一个如何以矢量化方式提取 3D 矩阵对角线的示例:

M = reshape(1:3*4*3,[3 4 3]);
[r,c,p] = size(M);

ind = bsxfun(@plus, (1:r+1:r*c)', (0:p-1).*r*c);
M(ind)

结果的每一列对应于每个切片的对角线元素(不一定是方阵):

>> M
M(:,:,1) =
     1     4     7    10
     2     5     8    11
     3     6     9    12
M(:,:,2) =
    13    16    19    22
    14    17    20    23
    15    18    21    24
M(:,:,3) =
    25    28    31    34
    26    29    32    35
    27    30    33    36

>> M(ind)
ans =
     1    13    25
     5    17    29
     9    21    33

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:

M = reshape(1:3*4*3,[3 4 3]);
[r,c,p] = size(M);

ind = bsxfun(@plus, (1:r+1:r*c)', (0:p-1).*r*c);
M(ind)

Each column of the result corresponds to the diagonal elements from each slice (doesn't have to be square matrix):

>> M
M(:,:,1) =
     1     4     7    10
     2     5     8    11
     3     6     9    12
M(:,:,2) =
    13    16    19    22
    14    17    20    23
    15    18    21    24
M(:,:,3) =
    25    28    31    34
    26    29    32    35
    27    30    33    36

>> M(ind)
ans =
     1    13    25
     5    17    29
     9    21    33
深海少女心 2024-12-18 08:56:19

给你:

n = 10;

% Make sample data
t = zeros(9,1,n);
t(:,1,1) = 1;
T = repmat(diag(ones(9,1)), [1 1 n]);

% Loop though to fill in t based on previous t and T
for i = 2:n
  t(:,1,i) = t(:,1,i-1) .* diag(T(:,:,i-1));
end

现在所有 t 都应该是 1。

Here you go:

n = 10;

% Make sample data
t = zeros(9,1,n);
t(:,1,1) = 1;
T = repmat(diag(ones(9,1)), [1 1 n]);

% Loop though to fill in t based on previous t and T
for i = 2:n
  t(:,1,i) = t(:,1,i-1) .* diag(T(:,:,i-1));
end

Now all of t should be 1.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文