MATLAB Recall that the multiplication of two matrices are de

MATLAB

Recall that the multiplication of two matrices are defined as the following: [A]_mn = [B]_mp[C]_pn A_ij = sigma^p_k = 1B_ikC_kj with correct dimensions, MATLAB * operator will give us the flexibility of doing these types of multiplications. Now, let us assume that this feature is not available in MATLAB. In other words, let us assume that MATLAB * can be used only for multiplying two \"scalar\" values. Use for loop and write a program that receives two matrices as inputs and returns the product of them as its output. If the dimensions of the matrices do not match according to the multiplication rule, the function should output an error message. Repeat part (a) for MATLAB. * (i.e. write a program that does the job of the MATLAB. * operator). Compare the elapsed time that it takes to run this program vs. simply using the * or. * operators. Experiment with a variety of conditions (different matrix sizes, etc) and comment on the differences in efficiency.

Solution

prompt = \'Enter A matrix row \';
m = input(prompt)
prompt = \'Enter A matrix col \';
p = input(prompt)
prompt = \'Enter B matrix row \';
q = input(prompt)
prompt = \'Enter B matrix col \';
r = input(prompt)

if p!=q
break;
  
for i=1:m
for j=1:p
prompt = \'Enter value \';
A(i,j) = input(prompt)
end
end

for i=1:q
for j=1:r
prompt = \'Enter value \';
B(i,j) = input(prompt)
end
end


fprintf([repmat(\'%d\\t\', 1, size(A, m*p)) \'\ \'], A\');
fprintf([repmat(\'%d\\t\', 1, size(A, q*r)) \'\ \'], B\');

C = A*B;
fprintf([repmat(\'%d\\t\', 1, size(C, m*r)) \'\ \'], C\');

for i=1:m
for j=1:r
D(i,j)=0
end
end


for i=1:m
for k=1:p
for j=1:r
D(i,k) = D(i,k) + A(i,j)*B(j,k);
end
end
end

fprintf([repmat(\'%d\\t\', 1, size(D, m*r)) \'\ \'], D\');

MATLAB Recall that the multiplication of two matrices are defined as the following: [A]_mn = [B]_mp[C]_pn A_ij = sigma^p_k = 1B_ikC_kj with correct dimensions,
MATLAB Recall that the multiplication of two matrices are defined as the following: [A]_mn = [B]_mp[C]_pn A_ij = sigma^p_k = 1B_ikC_kj with correct dimensions,

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site