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

时间:2020-01-05 03:14:48

标签: matlab matrix product matrix-multiplication

我有一个常数,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进行绘制?

1 个答案:

答案 0 :(得分:3)

由于不清楚您要问的是什么,因此我在同一答案中提供了两种解决方案。有关更多信息,请参见下面的代码中的注释。

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');

这将导致:

enter image description here

相关问题