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

Using MATLAB: Here is what I have: What am I doing wrong? 5. Recall that if A is

ID: 3111123 • Letter: U

Question

Using MATLAB:

Here is what I have:

What am I doing wrong?

5. Recall that if A is an m × n matrix and B is a p × q matrix, then the product C = AB is defined if and only if n = p, in which case C is an m × q matrix. (a) Write a function M-file that takes as input two matrices A and B, and as output produces the product by columns of the two matrix. For instance, if A is 3 x 4 and B is 4x 5, the product is given by the matrix The function file should work for any dimension of A and B and it should perform a check to see if the dimensions match (Hint: use a for loop to define the columns of C). Call the file columnproduct.m. Test your function on a random 2 × 3 matrix A and a random 3 × 2 matrix B . Compare the output with A*B. Repeat with 3 × 4 and 4 × 2 matrices and with 3 × 4 and 2 × 4 matrices. Include in your lab report the function M-file and the output obtained by running it

Explanation / Answer

Filename: mycolumnproduct.m

function C = mycolumnproduct(A,B)
%MYCOLUMNPRODUCT Matrix multiplication column wise product

[m,n] = size(A);
[p,q] = size(B);

if n == p
C = zeros(m,q);
for i = 1:q
C(:,i) = A*B(:,i);
end
else
disp('dimensions do not match');
C = [];

end

Testing the code

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

A =

1 2 3
3 4 1

>> B = [2 4;3 6;1 6]

B =

2 4
3 6
1 6

>> C = mycolumnproduct(A,B)

C =

11 34
19 42

>> A*B

ans =

11 34
19 42

The code seems to be working fine. The code remains the same as you have provided for the most part. The error in your code occurs in Line 9. The assignment C(i) is wrong because we are assigning entire columns to the matrix C.

The RHS is a column vector. So the assigning should be C(:,i) not C(i). Second on the RHS of the same line the matrix vector product should be A*B(:,i). But instead you have assigned a fixed matrix vector product A*B(:,q).

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