MATLAB: Getting errors and incorrect matrix form.

dimensionfunctionmatrixmultiplicationrow productscriptunexpected

Hello I am relatively new to the matlab program and am struggling with this assignment.
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 rows of the two matrices. For instance, if A is 3 4 and B is 4 5, the product AB is given by the matrix C = [A(1,:)*B; A(2,:)*B; A(3,:)*B] 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 rows of C).
I have been able to clear my function of errors so that it returns an answer, however its not coming back as a matrix of the correct dimensions. I get the answer returned row by row it appears. My function is written as follows:
I have typed the following elements into matrices A and B in the command window with the ans following.
I expected a 2×2 matrix with elements [14,14;42,42].
Thanks for the help!

Best Answer

You forgot to index ‘C’ !
C(i,:)=A(i,:)*B %Computes every row of A times matrix B
That works.
(It’s best not to use i and j as variables, because MATLAB uses them as its imaginary operators. Not a problem here, but it can cause confusion if you’re dealing with complex numbers.)
Related Question