In: Advanced Math
4. The product y = Ax of an m n matrix A times a vector x = (x1; x2; : : : ; xn)T can be computed row-wise as y = [A(1,:)*x; A(2,:)*x; ... ;A(m,:)*x]; that is y(1) = A(1,:)*x y(2) = A(2,:)*x ... y(m) = A(m,:)*x Write a function M-file that takes as input a matrix A and a vector x, and as output gives the product y = Ax by row, as denoted above (Hint: use a for loop to denote each entry of the vector y.) Your M-file should perform a check on the dimensions of the input variables A and x and return a message if the dimensions do not match. Call the file myrowproduct.m. Note that this le will NOT be the same as the myproduct.m example. Test your function on a random 5x2 matrix A and a random 2x1 vector x. Compare the output with A*x. Repeat with a 3x5 matrix and a 5x1 vector and with a 3x5 matrix and a 1x5 vector. Use the command rand to generate the random matrices for testing. Include in your lab report the function M-file and the output obtained by running it.
5. Recall that if A is an m n matrix and B is a p q matrix, then the product C = AB is denoted 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 3x4 and B is 4x5, the product is
given by the matrix C = [A*B(:,1), A*B(:,2), A*B(:,3), A*B(:,4),
A*B(:,5)]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
denote each column of C). Call the file columnproduct.m.Test your
function on a random 5x3 matrix A and a random 3x5 matrix B .
Compare the output with A*B. Repeat with 3 x 6 and 6 x 4 matrices
and with 3 x 6 and 4 x 6 matrices.Use the command rand to generate
the random matrices for testing. Include in your lab report the
function M-file and the output obtained by running it.
(b) 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 x 4 and B is 4x5, 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 denote each row of C). Call the file rowproduct.m. Test
your function on a random 5x3 matrix A and a random 3x5 matrix B .
Compare the output with A*B. Repeat with 3 x 6 and 6 x 4 matrices
and with 3 x 6 and 4 x 6 matrices. Use the command rand to generate
the random matrices for testing.
Include in your lab report the function M-file and the output
obtained by running it.
4) Required Matlab code with explanatory comments is given below:
function B=myrowproduct(A,x)
[Ar,Ac]=size(A);
[xr,xc]=size(x);
if not(Ac==xr)
msg='Matrices not compatible';
error(msg); %throw this message if not
compatible
else
B=zeros([Ar,xc]);
for i=1:Ar
for j=1:xc
for k=1:Ac
B(i,j)=B(i,j)+A(i,k)*x(k,j); %matrix multiplication using for
loops
end
end
end
end
end
Sample usages:
Hope this was helpful. Please do leave a positive rating if you liked this answer. Thanks and have a good day!