Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a MATLAB program (script) to multiply two matrices together. Do not use th

ID: 3565773 • Letter: W

Question

Write a MATLAB program (script) to multiply two matrices together. Do not use the MATLAB matrix multiplication function, A*B. Make the program general so that it will work with matrices of any dimension. Check that the inner dimensions of the matrices arc the same before performing the multiplication. If the inner dimensions do not match, display an error and stop.You can use the MATLAB function §ig£() to get the number of rows and columns in the matrices to be multiplied. Use the inputfl function to get the matrix elements. Matrices will be entered in the form [1 2 3^,5 6;7 8 9;...].Be sure to includc comments. The first comment should describe the purpose of the program and the data to be entered. Other comments should be included in the program to describe your calculations. Five percent of your program grade will be based on comments.Use this equation to help crcatc your script, where A and B arc the matrices to be multiplied and C is the product matrix.

Explanation / Answer

A=[3 4 1;
    2 3 4];

B=[ 1 2 3;
     4 5 6;
     7 8 9];

[nRow1,nCol1]=size(A);
[nRow2,nCol2]=size(B);

if nCol1 ~= nRow2
    error('inner dimensions must match');
end

C = zeros(nRow1,nCol2);

for i = 1:nRow1
     for j = 1:nCol2
         for k = 1:nCol1
             C(i,j) = C(i,j) + A(i,k)*B(k,j);
         end
     end
end

C
-----------

This gives

C =

     26 34 42
     42 51 60

To verify

A*B

ans =

     26 34 42
     42 51 60

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote