Arrays Matlab:乘积的乘法和求和

Arrays Matlab:乘积的乘法和求和,arrays,matlab,matrix,matrix-multiplication,Arrays,Matlab,Matrix,Matrix Multiplication,如何有效地对10对数组执行矩阵乘法,并对乘法矩阵的相应结果求和?例如: 创建两个阵列,A1和B1 A1 = [1 3 5; 2 4 7]; B1 = [-5 8 11; 3 9 21; 4 0 8]; C1 = A1*B1 C1 = 24 35 114 30 52 162 计算A1和B1的乘积 A1 = [1 3 5; 2 4 7]; B1 = [-5 8 11; 3 9 21; 4 0 8]; C1 = A1*B1 C1 = 24

如何有效地对10对数组执行矩阵乘法,并对乘法矩阵的相应结果求和?例如:

创建两个阵列,A1和B1

A1 = [1 3 5; 2 4 7];
B1 = [-5 8 11; 3 9 21; 4 0 8];
C1 = A1*B1

C1 =

    24    35   114
    30    52   162
计算A1和B1的乘积

A1 = [1 3 5; 2 4 7];
B1 = [-5 8 11; 3 9 21; 4 0 8];
C1 = A1*B1

C1 =

    24    35   114
    30    52   162
创建另外两个阵列,A2和B2

A2 = [7 9 11; 3 5 6];
B2 = [-1 2 3; 4 5 6; 4 1 8];
C2 = A2*B2



sum_of_products1 = sum(C1,C2);
sum_of_products2 = sum(C3,C4); and so on till 


    .
    .
    .
sum_of_products5 = sum(C9,C10);
计算A2和B2的乘积

A2 = [7 9 11; 3 5 6];
B2 = [-1 2 3; 4 5 6; 4 1 8];
C2 = A2*B2



sum_of_products1 = sum(C1,C2);
sum_of_products2 = sum(C3,C4); and so on till 


    .
    .
    .
sum_of_products5 = sum(C9,C10);
更新

根据回复,我输入向量。求和的结果不正确

clc;
clear all;

%VECTORS
A1(:,1) = [1 2 3 4].';
A1(:,2) = [5 6 7 8].';

%// Concatenate all An and Bn arrays along third dim to have A, B as 3D arrays
A = cat(3,A1(:,1),A1(:,2));
B = cat(3,A1(:,1)',A1(:,2)');

%// You may clear A1, A2,...A10, B1, B2.., B10 at this point for a cleaner workspace

%// Get the product values into a 3D array
multvals = squeeze(sum(bsxfun(@times,permute(A,[1 2 4 3]),permute(B,[4 1 2 3])),2))

%// Finally get the sum of product values
sumvals = squeeze(sum(reshape(multvals,size(A,1),size(A,2),2,[]),3))
回答

多变量(:,:,1)=

多变量(:,:,2)=

盛宴=

 3     7    55    75
 6    14    66    90
 9    21    77   105
12    28    88   120
正确答案应该是

s1 = multvals(:,:,1);
s2 = multvals(:,:,2); 

sumvals = s1 + s2;

sumvals = 
26  32  38  44
32  40  48  56
38  48  58  68
44  56  68  80

您可以将它们连接成两个三维阵列并使用它们,而不是遍历所有这些阵列

%// Concatenate all An and Bn arrays along third dim to have A, B as 3D arrays
A = cat(3,A1,A2,...)
B = cat(3,B1,B2,...)

%// You may clear A1, A2,..., B1, B2..., at this point for a cleaner workspace

%// Get the product values into a 3D array
multvals = sum(bsxfun(@times,permute(A,[1 2 4 3]),permute(B,[4 1 2 3])),2)

%// Finally get the sum of product values
[m1,m2,m3,m4] = size(multvals);
sumvals = squeeze(sum(reshape(multvals,m1,m2,m3,2,[]),4))
multvals
的最后一个维度中的每个片都对应于这些
C
数组中的每个。最后,
3D
的每个
summals
片段将对应于每个
产品的总和
数组


样本使用向量和矩阵运行 1)向量大小写

clear all;clc;

%// VECTORS
A1 = [1 2 3 4].';
B1 = [5 6 7 8];

A2 = [11 12 13 14].';
B2 = [15 16 17 18];

A = cat(3,A1,A2);
B = cat(3,B1,B2);

multvals = sum(bsxfun(@times,permute(A,[1 2 4 3]),permute(B,[4 1 2 3])),2);
[m1,m2,m3,m4] = size(multvals);
sumvals = squeeze(sum(reshape(multvals,m1,m2,m3,2,[]),4))

solution_with_approach_from_question = A1*B1 + A2*B2 
%// this should yield values identical to sumvals(:,:,1)
clear all;clc;

%// MATRICES
A1 = [1 3 5; 2 4 7];
B1 = [-5 8 11; 3 9 21; 4 0 8];

A2 = [7 9 11; 3 5 6];
B2 = [-1 2 3; 4 5 6; 4 1 8];

A = cat(3,A1,A2);
B = cat(3,B1,B2);

multvals = sum(bsxfun(@times,permute(A,[1 2 4 3]),permute(B,[4 1 2 3])),2);
[m1,m2,m3,m4] = size(multvals);
sumvals = squeeze(sum(reshape(multvals,m1,m2,m3,2,[]),4))

solution_with_approach_from_question = A1*B1 + A2*B2 
%// this should yield values identical to sumvals(:,:,1)
输出-

sumvals =
   170   182   194   206
   190   204   218   232
   210   226   242   258
   230   248   266   284
solution_with_approach_from_question =
   170   182   194   206
   190   204   218   232
   210   226   242   258
   230   248   266   284
sumvals =
    97   105   277
    71    89   249
solution_with_approach_from_question =
    97   105   277
    71    89   249
2)矩阵情况

clear all;clc;

%// VECTORS
A1 = [1 2 3 4].';
B1 = [5 6 7 8];

A2 = [11 12 13 14].';
B2 = [15 16 17 18];

A = cat(3,A1,A2);
B = cat(3,B1,B2);

multvals = sum(bsxfun(@times,permute(A,[1 2 4 3]),permute(B,[4 1 2 3])),2);
[m1,m2,m3,m4] = size(multvals);
sumvals = squeeze(sum(reshape(multvals,m1,m2,m3,2,[]),4))

solution_with_approach_from_question = A1*B1 + A2*B2 
%// this should yield values identical to sumvals(:,:,1)
clear all;clc;

%// MATRICES
A1 = [1 3 5; 2 4 7];
B1 = [-5 8 11; 3 9 21; 4 0 8];

A2 = [7 9 11; 3 5 6];
B2 = [-1 2 3; 4 5 6; 4 1 8];

A = cat(3,A1,A2);
B = cat(3,B1,B2);

multvals = sum(bsxfun(@times,permute(A,[1 2 4 3]),permute(B,[4 1 2 3])),2);
[m1,m2,m3,m4] = size(multvals);
sumvals = squeeze(sum(reshape(multvals,m1,m2,m3,2,[]),4))

solution_with_approach_from_question = A1*B1 + A2*B2 
%// this should yield values identical to sumvals(:,:,1)
输出-

sumvals =
   170   182   194   206
   190   204   218   232
   210   226   242   258
   230   248   266   284
solution_with_approach_from_question =
   170   182   194   206
   190   204   218   232
   210   226   242   258
   230   248   266   284
sumvals =
    97   105   277
    71    89   249
solution_with_approach_from_question =
    97   105   277
    71    89   249

至少对于工作空间效率,考虑使用3D阵列来包含所有这5对阵列。对于您的代码和预期的输出,您已经在为一个混乱的工作区调用15个变量了。@Divakar:我不熟悉使用3D数组。你能告诉我如何循环乘法和求和吗?谢谢查看发布的解决方案?谢谢您的回复。你能解释一下permute[1 2 4 3]和其他数字的含义吗?另外,同样的方法也适用于向量,比如A1=[1 2 3]”,B1是[5 6 7]?当输入是一维向量时,我检查了代码。求和的结果不正确。我已在我的问题中公布了结果。您能看一看吗?@SrishtiM如果您想在最低级别上测试解决方案中的代码,请看这里,看看这是否有意义-@SrishtiM,我很抱歉,但我不明白您在问题的更新部分试图做什么。最初,你有A1,A2,。。。B1,B2。。。数组和我的解决方案建议将这些数组连接到一个3D数组中,而在更新中,您将a的列连接到
a=cat(3,A1(:,1),A1(:,2))。这两件事完全是两码事。更重要的是,最初是将
A1
B1
相乘,以此类推。您在更新中是如何做到这一点的?@SrishtiM以及官方声明,
cat
只是沿着指定维度连接元素。因为我们知道这里处理的是1D(向量)和2D(矩阵),所以第三维是“空的”,元素可以沿着该维连接。这基本上就是猫在这里所做的。关于你的时间序列问题,我认为这与这里的上下文无关,可以作为一个单独的问题。祝你好运