How to do matrix or table multiplication?

3 ビュー (過去 30 日間)
raghavendra kandukuri
raghavendra kandukuri 2019 年 9 月 6 日
Hi,
I have this two tables or matrix, where i am looking to perform the addition of 1st element in the first matrix of first row with the all element in the first row of second matrix and similarly with the second element in the first matrix of first row with the all the elements of first row in second matrix. Similarly with the second and third row elements.
Is there any easy way or formula to do this? even if the size of the matrix changes
Example:
IntakeTable = [0 3 2
4 2 2
4 4 4 ];
ExhaustTable = [0 3 2
4 2 2
2 2 2 ];
resultTable =[0+0 0+3 0+2 3+0 3+3 3+2 2+0 2+3 2+2
4+4 4+2 4+2 2+4 2+2 2+2 2+4 2+2 2+2
4+2 4+2 4+2 4+2 4+2 4+2 4+2 4+2 4+2]

採用された回答

Walter Roberson
Walter Roberson 2019 年 9 月 6 日
resultTable = kron(IntakeTable,ones(1,size(ExhaustTable,2))) + repmat(ExhaustTable,1,size(IntakeTable,2));
  1 件のコメント
raghavendra kandukuri
raghavendra kandukuri 2019 年 9 月 6 日
Thank you @ Walter

サインインしてコメントする。

その他の回答 (2 件)

Bjorn Gustavsson
Bjorn Gustavsson 2019 年 9 月 6 日
Looping seems simplest and most easily readable for humans:
szI = size(IntakeTable);
szE = size(ExhaustTable);
resultTable = zeros(size(ExhaustTable,1),size(ExhaustTable,2)*size(IntakeTable))
for i1 = 1:szI(2),
resultTable(:,(1:szE,2)+(i1-1)*szE(2)) = repmat(IntakeTable(:,i1),1,szE(2)) + ...
ExhaustTable;
end
HTH
  1 件のコメント
raghavendra kandukuri
raghavendra kandukuri 2019 年 9 月 6 日
Thank you@ Bjorn Gustavsson

サインインしてコメントする。


Adam Danz
Adam Danz 2019 年 9 月 6 日
編集済み: Adam Danz 2019 年 9 月 6 日
resultTable = repmat(ExhaustTable,1,3) + repelem(IntakeTable,1,3)
Or, if the number of columns vary,
resultTable = repmat(ExhaustTable,1,size(IntakeTable,2)) + repelem(IntakeTable,1,size(ExhaustTable,2))
  1 件のコメント
raghavendra kandukuri
raghavendra kandukuri 2019 年 9 月 6 日
Thank you@ Adam

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by