Matrix Product Function with dot product

6 ビュー (過去 30 日間)
Ashley
Ashley 2013 年 10 月 16 日
回答済み: Matt J 2013 年 10 月 17 日
I'm trying to write a function that multiplies matrices from inner products this is what i have right now. I would like to know if its correct or wrong. any advice would help, thank you!
function [C] =MatrixProduct(A,B)
% This is a program designed to multiply two matrices together by using the inner product.
% Call syntax: MatrixProduct(A,B)
% Input: an mxp matrix A and an pxn matrix B
% Output: the product AB which is an mxn matrix C
m= size(A,1); % gets the row size of matrix A
p= size(A,2); % gets the column size of matrix A
n= size(B,2); % gets the column size of matrix B
f= size(B,1); % gets the row size of matrix B
if f==p
C=zeros(m,n); % initializes the variable C
for i=1:m
for j=1:n
C(i,j)=C(i,j) + A(i,:) * B(:,j); % updates C
end % ends loop
end % ends loop
ans = C % prints the final answer C
else
error('The dimensions of the matrices must agree.')
end

回答 (1 件)

Matt J
Matt J 2013 年 10 月 17 日
My advice would be to test it by comparing the result with MATLAB's built-in matrix multiplication A*B

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by