Question

In: Advanced Math

4. The product y = Ax of an m n matrix A times a vector x...

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.

Solutions

Expert Solution

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!


Related Solutions

4. The product y = Ax of an m × n matrix A times a vector...
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 defined above (Hint: use...
Let A be an m x n matrix. Prove that Ax = b has at least...
Let A be an m x n matrix. Prove that Ax = b has at least one solution for any b if and only if A has linearly independent rows. Let V be a vector space with dimension 3, and let V = span(u, v, w). Prove that u, v, w are linearly independent (in other words, you are being asked to show that u, v, w form a basis for V)
Problem 4: Suppose M is a random matrix, and x is a deterministic (fixed) column vector....
Problem 4: Suppose M is a random matrix, and x is a deterministic (fixed) column vector. Show that E[x' M x] = x' E[M] x, where x' denotes the transpose of x.
Given two functions, M(x, y) and N(x, y), suppose that (∂N/∂x − ∂M/∂y)/(M − N) is...
Given two functions, M(x, y) and N(x, y), suppose that (∂N/∂x − ∂M/∂y)/(M − N) is a function of x + y. That is, let f(t) be a function such that f(x + y) = (∂N/∂x − ∂M/∂y)/(M − N) Assume that you can solve the differential equation M dx + N dy = 0 by multiplying by an integrating factor μ that makes it exact and that it can also be written as a function of x + y,...
Suppose A is an n × n matrix with the property that the equation Ax =...
Suppose A is an n × n matrix with the property that the equation Ax = b has at least one solution for each b in R n . Explain why each equation Ax = b has in fact exactly one solution
2.48. Is it true that {ax + by + cz|x, y, z ∈ Z} = {n...
2.48. Is it true that {ax + by + cz|x, y, z ∈ Z} = {n · gcd(a, b, c)|n ∈ Z}? 2.49. What are all the integer values of e for which the Diophantine equation 18x + 14y + 63z = e has an integer solution. Find a solution for each such e. 2.50. For integers a, b and k > 0, is it true that a | b iff a^k | b^k ?
Let A be an m x n matrix and b and x be vectors such that...
Let A be an m x n matrix and b and x be vectors such that Ab=x. a) What vector space is x in? b) What vector space is b in? c) Show that x is a linear combination of the columns of A. d) Let x' be a linear combination of the columns of A. Show that there is a vector b' so that Ab' = x'.
for the system equation of x' = Ax   if Coefficients Matrix A be ? = [...
for the system equation of x' = Ax   if Coefficients Matrix A be ? = [ 5 −5 −5 −1 4 2 3 −5 −3 ] , find the basic matrix
Suppose C is a m × n matrix and A is a n × m matrix....
Suppose C is a m × n matrix and A is a n × m matrix. Assume CA = Im (Im is the m × m identity matrix). Consider the n × m system Ax = b. 1. Show that if this system is consistent then the solution is unique. 2. If C = [0 ?5 1 3 0 ?1] and A = [2 ?3   1 ?2    6 10] ,, find x (if it exists) when (a) b =[1...
can a vector with dimensions R^N and a vector with dimensions R^N+1 be a matrix? and...
can a vector with dimensions R^N and a vector with dimensions R^N+1 be a matrix? and if so what would it dimension size be?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT