PROGRAMMING IN MATLAB. Write a function with the header: function [C ] = myMatMa
ID: 1721048 • Letter: P
Question
PROGRAMMING IN MATLAB.
Write a function with the header: function [C ] = myMatMatProd(A, B),
which uses three nested for loops to multiply matrices A and B. If A is pxq and B is rxs, C will be pxs. If A and B are not compatible, the function should return an error code of -1.
Test Cases:
You may check your work against Matlab’s built in matrix multiplication.
For example,
>> A = rand(3,7);
>> B = rand(7,4);
>> C = A*B
C =
0.9509 1.9375 1.5620 1.2809
1.4532 1.2605 1.7815 1.3751
1.0587 1.4412 1.6304 1.1006
Explanation / Answer
function [C] = myMatMatProd(A,B)
[RA,CA] = size(A);
[RB,CB] = size(B);
if CA~=RB
C=-1;
error('Matrices incompatible for multiplication. Error Code : %d',-1);
end
C = zeros(RA,CB);
for row=1:RA
for col = 1:CB
theSum=0;
for k= 1 : CA
theSum = theSum + A(row,k)*B(k,col);
end
C(row,col)=theSum;
end
end
end
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.