Multiplying 2 Matrices A and B from Scratch (not built in function

21 ビュー (過去 30 日間)
Abdalla Khan
Abdalla Khan 2019 年 6 月 16 日
コメント済み: Walter Roberson 2024 年 10 月 8 日
How can i write a matlab function which accepts two Matrices A and B and from scratch(indexing - not built in functions) multiplies them together. in the case that they can not be multiplied together simply return an empty matrix ([])
  1 件のコメント
dpb
dpb 2019 年 6 月 16 日
Obviously homework...show your work to date and where, specifically, you got stuck on a Matlab-related question.
Of course, you start with the definition of what matrix multiplication is...

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

回答 (1 件)

Manvi Goel
Manvi Goel 2019 年 6 月 17 日
Here you go
function result = mult(a, b)
[r1, c1] = size(a);
[r2, c2] = size(b);
if( c1 == r2)
result = zeros(r1, c2);
for i = 1: r1
for j =1:c2
for k = 1:r2
result(i,j) = result(i,j) + a(i,k) * b(k,j);
end
end
end
else
result = [];
end
disp(result)
end
  3 件のコメント
Mark
Mark 2024 年 10 月 8 日
@madhan buzzkill
Walter Roberson
Walter Roberson 2024 年 10 月 8 日
It's okay in this case, because the provided code is incorrect.

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

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by