计算越来越多的矩阵的乘积

Compute the product of an increasing number of matrices

我有一个常量整数 m,它的值是已知的。我们定义一个名为M的矩阵,其元素如下:

      | M_11  M_12 |
M_i ≜ |            |
      | M_21  M_22 |

最初,生成 m 个 2x2 矩阵(即 M1,...,M5)。然后,对于 1m 之间的每个整数,我需要计算一个矩阵,使得

for m=1, P1 = M1
for m=2, P2 = M1.M2
for m=3, P3 = M1.M2.M3 
and so on

如何提取乘积矩阵的 P_11 并将其与 m 作图?

由于不是很清楚你在问什么,我在同一个答案中提供了两个解决方案。更多信息,请查看下方代码中的注释。

function q59596709
%% Definitions:
rng(59596709); % for reproducibility
m = 5;
SZ = 2;

%% Preallocation
[P_arr,P_mat] = deal(zeros(m,1)); % preallocation

%% Generate data:
M = rand(SZ,SZ,m);

%% Perform ARRAY multiplication
for ind1 = 1:m
  tmp = prod(M(:,:,1:ind1),3);
  P_arr(ind1) = tmp(1); % get the first (i.e. 1,1) element
end

%% Perform MATRIX multiplication
tmp = eye(SZ);
for ind1 = 1:m
  tmp = tmp * M(:,:,ind1);
  P_mat(ind1) = tmp(1);  % get the first (i.e. 1,1) element
end

%% Show the first element:
figure(); plot(1:m, P_arr, 1:m, P_mat); 
xlabel('m'); ylabel('value'); set(gca, 'FontSize', 14);
legend('M_{11}, ARRAY multiplication', 'M_{11}, MATRIX multiplication');

这导致: