function P = mat_prod(A,B) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % mat_prod - matrix multiplication % % Inputs % % A, B - matrices to be multiplied % % Output: % % P - resulting product %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% [a1,a2] = size(A); [b1,b2] = size(B); if ~(a2 == b1), error('A and B must have the same inner dimensions'); end P = zeros([a1 b2]); for i = 1:a1 for j = 1:b2 for k = 1:a2 P(i,j) = P(i,j) + A(i,k)*B(k,j); end end end