Matlab的:乘法和产品的总和 [英] Matlab: Multiplication and sum of products

查看:218
本文介绍了Matlab的:乘法和产品的总和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎样才能effeciently进行矩阵乘法10对阵列,总结乘法矩阵相应的结果?例如:

创建两个数组,A1和B1。

  A1 = [1 3 5; 2 4 7];
B1 = [-5 8月11日; 3 9月21日; 4 0 8]。

计算A1和B1的产品

  C1 = A1 * B1C1 =    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];

计算A2和B2的产品

  C2 = A2 * B2sum_of_products1 =总和(C1,C2);
sum_of_products2 =总和(C3,C4);等等,直到
    。
    。
    。
sum_of_products5 =总和(C9,C10);

更新

根据的答复,I输入矢量。求和的结果来了不正确。

  CLC;
清除所有;%向量
A1(:,1)= [1 2 3 4]';
A1(:,2)。= [5 6 7 8]';//%串连所有的和BN沿着昏暗的第三阵列有A,B三维阵列
A =猫(3,A 1(:,1),A 1(:,2));
B =猫(3,A 1(:,1)',A 1(:,2)');%//你可能会清除A1,A2,... A10,B1,B2 ...,B10在这一点上要清洁器工作区%//获取产品价值成3D阵列
multvals =挤压(SUM(bsxfun(@倍,置换(A,[1 2 4 3]),置换(B,[4 1 2 3])),2))//%得到最终产品价值的总和
sumvals =挤压(SUM(重塑(multvals,尺寸(A,1),尺寸(A,2),2,[]),3))

multvals(:,:,1)=

  1 2 3 4
 2 4 6 8
 3 6 9 12
 4 8 12 16

multvals(:,:,2)=

  25 30 35 40
30 36 42 48
35 42 49 56
40 48 56 64

sumvals =

  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


解决方案
而不是通过所有的阵列循环的

,你可以将它们连接起来成两个3D阵列,并与他们合作。

  //%串连所有的和BN沿着昏暗的第三阵列有A,B三维阵列
A =猫(3,A1,A2,...)
B =猫(3,B1,B2,......)%//你可能会清除A1,A2,......,B1,B2 ...,在这一点上要清洁器工作区%//获取产品价值成3D阵列
multvals =总和(bsxfun(@倍,置换(A,[1 2 4 3]),置换(B,[4 1 2 3])),2)//%得到最终产品价值的总和
[M1,M2,M3,M4 =尺寸(multvals);
sumvals =挤压(总和(重塑(multvals,M1,M2,m3,2,[]),4))

multvals 的最后一维每个切片对应于每个那些 C 阵列。在结束 sumvals 的每个 3D 片将对应于每个 sum_of_products 阵列。


样品向量和矩阵

运行

1)向案例

 清除所有;中图分类号;//%向量
A1 = [1 2 3 4]'。
B1 = [5 6 7 8];A2 = [11 12 13 14]'。
B2 = [15 16 17 18];A =猫(3,A1,A2);
B =猫(3,B1,B2);multvals =总和(bsxfun(@倍,置换(A,[1 2 4 3]),置换(B,[4 1 2 3])),2);
[M1,M2,M3,M4 =尺寸(multvals);
sumvals =挤压(总和(重塑(multvals,M1,M2,m3,2,[]),4))solution_with_approach_from_question = A1 * B1 + A2 * B2
//%这应该产生相同的值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

2)矩阵的情况下

 清除所有;中图分类号;//%矩阵
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 =猫(3,A1,A2);
B =猫(3,B1,B2);multvals =总和(bsxfun(@倍,置换(A,[1 2 4 3]),置换(B,[4 1 2 3])),2);
[M1,M2,M3,M4 =尺寸(multvals);
sumvals =挤压(总和(重塑(multvals,M1,M2,m3,2,[]),4))solution_with_approach_from_question = A1 * B1 + A2 * B2
//%这应该产生相同的值sumvals(:,:,1)

输出 -

  sumvals =
    97 105 277
    71 89 249
solution_with_approach_from_question =
    97 105 277
    71 89 249

How can I effeciently perform matrix multiplication for 10 pairs of arrays and sum the corresponding result of multiplication matrices? For example:

Create two arrays, A1 and B1.

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

Calculate the product of A1 and B1.

C1 = A1*B1

C1 =

    24    35   114
    30    52   162

Create another two arrays, A2 and B2.

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

Calculate the product of A2 and B2.

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

UPDATE

Based on the reply, I input vectors. The result of summation is coming incorrect.

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

Answer

multvals(:,:,1) =

 1     2     3     4
 2     4     6     8
 3     6     9    12
 4     8    12    16

multvals(:,:,2) =

25    30    35    40
30    36    42    48
35    42    49    56
40    48    56    64

sumvals =

 3     7    55    75
 6    14    66    90
 9    21    77   105
12    28    88   120

The correct answer should have been

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

解决方案

Instead of looping through all those arrays, you can concatenate them into two 3D arrays and work with them.

%// 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))

Each slice in the last dimension of multvals would correspond to each of those C arrays. At the end each 3D slice of sumvals would correspond to each sum_of_products array.


Samples runs with vectors and matrices

1) Vector case

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)

Output -

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

2) Matrix case

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)

Output -

sumvals =
    97   105   277
    71    89   249
solution_with_approach_from_question =
    97   105   277
    71    89   249

这篇关于Matlab的:乘法和产品的总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆