Write a MATLAB program (script) to multiply two matrices together. Do not use th
ID: 3566077 • 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 are the same before performing the multiplication. If the inner dimensions do not match, display an error and stop. You can use the MATLAB function sizeO to get the number of rows and columns in the matrices to be multiplied. Use the input() function to get the matrix elements. Matrices will be entered in the form [1 2 3;4 5 6;7 8 9;.. . ]. Be sure to include 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 create your script, where A and B are 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
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.