As you know, Matlab has the capability of multiplying two matrices with only a s
ID: 2082682 • Letter: A
Question
As you know, Matlab has the capability of multiplying two matrices with only a single line of code. However, your cruel teacher is now asking you to do it the hard way. Write a program that multiplies two rectangular matrices, A and B, and stores the result in C. You should also check and make sure the matrices can be multiplied (if A is m times p then B must be p times n, and then C will be a m times n matrix). Test your program with the following, but it should work with any two matrices: A = [2 2 16 4 8 1] and B = [2 -3 4 6 1 2] To be clear, you must use loops to do this. If you write C=A*B as your program, don't expect to get any credit. The mathematical expression is C_ij = sigma_k=1^p A_ik B_kjExplanation / Answer
MATLAB CODE
%% This program multiplies two matrices using basic mathematical operations
close all;
clear all;
clc;
display('This program multiplies two matrices');
display('Enter the two matrices to be multiplied');
A=input('enter matrix A: ');
B=input('enter matrix B: ');
%% check dimensions of the entered matrices to see if they can be multiplied
[row_A, column_A]=size(A);
[row_B, column_B]=size(B);
if column_A~=row_B
display('Matrix dimensions of the matrices A and B are incompatible for multiplication');
else
%% initialise matrix C to store A*B
C=zeros(row_A,column_B);
%% matrix multiplication begins
for i=1:row_A
for j=1:column_B
for k=1:column_A %% or k=1:column_B
C(i,j)=C(i,j)+A(i,k)*B(k,j);
end
end
end
display(' multiplication of A and B matrices results in matrix C');
display(C);
end
OUTPUT
This program multiplies two matrices
Enter the two matrices to be multiplied
enter matrix A: [2 2;16 4;8 1]
enter matrix B: [2 -3 4;6 1 2]
multiplication of A and B matrices results in matrix C
C =
16 -4 12
56 -44 72
22 -23 34
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.