MATLAB: I have to make a code that does matrix multiplication. This is what I have so far but I’m not sure why it’s not working. I cannot figure out if the for loops for k, i, and j are correct.

for loops

function [C] = mult(A, B)
[n1,m1]=size(A);
[n2,m2]=size(B);
if m1~=n2
error('Matrix dimensions do not match');
end
for k=1:n1
for i = 1:n1
for j=1:m2
C(i,j)=A(i,k)*B(k,j);
end
end
end
end

Best Answer

I suggest you go back to the definition of matrix multiplication. This involves a sum which appears nowhere in your code.