Question

In: Computer Science

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 a for loop to define 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 file will NOT be the same as the myproduct.m example. Test your function on a random 5 × 3 matrix A and a random 3 × 1 vector x. Compare the output with A*x. Repeat with a 3 × 6 matrix and a 6 × 1 vector and with a 3 × 6 matrix and a 1 × 6 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.

Solutions

Expert Solution

.m file

function y = myrowproduct(A,x)
    y=[];
   [r1 c1]=size(A);
   [r2 c2]=size(x);
    if c1~=r2
       disp('dimensions not agree');
       return;
    else
    for i=1:r1
       y(i,1)=A(i,:)*x;
     end
    end

 end

command window output:

>> clear all
 >> A = [1 2 3;4 5 6]

A =

1 2 3
 4 5 6

>> b = [1;2;3]

b =

1
 2
 3

>> m1 =myrowproduct(A,b)

m1 =

14
 32

>> C = [1 2 3 4;4 5 6 7;2 5 4 7]

C =

1 2 3 4
 4 5 6 7
 2 5 4 7

>> x = [4;2;7;8]

x =

4
 2
 7
 8

>> m1 =myrowproduct(C,x)

m1 =

61
 124
 102

>> x = x'

x =

4 2 7 8

>> m1 =myrowproduct(C,x)

dimensions not agree

m1 =

[]

>>
Let me know if you have any doubts or if you need anything to change. 

If you are satisfied with the solution, please leave a +ve feedback : ) Let me know for any help with any other questions.

Thank You!
==========================================================================

Related Solutions

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...
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)
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
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?
Let A be an m × n matrix and B be an m × p matrix....
Let A be an m × n matrix and B be an m × p matrix. Let C =[A | B] be an m×(n + p) matrix. (a) Show that R(C) = R(A) + R(B), where R(·) denotes the range of a matrix. (b) Show that rank(C) = rank(A) + rank(B)−dim(R(A)∩R(B)).
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.
A linear system of equations Ax=b is known, where A is a matrix of m by...
A linear system of equations Ax=b is known, where A is a matrix of m by n size, and the column vectors of A are linearly independent of each other. Please answer the following questions based on this assumption, please explain all questions, thank you~. (1) Please explain why this system has at most one solution. (2) To give an example, Ax=b is no solution. (3) According to the previous question, what kind of inference can be made to the...
maximumST is a function that should take an n × n vector (representing the adjacency matrix...
maximumST is a function that should take an n × n vector (representing the adjacency matrix of a graph) as input and return the value of the maximum spanning tree of the graph defined by the n × n vector. Code: #include <vector> #include <cmath> #include <iostream> #include <cstdio> using namespace std; double maximumST( vector< vector<double> > adjacencyMatrix ){   vector<int> row(4,-1);   vector< vector<int> > matrix(5,row);   cerr << matrix.size() << endl;   cerr << matrix[0].size() << endl;   for( int i = 0;...
What is the difference between multiplying a matrix times a vector and multiplying two matrices?
What is the difference between multiplying a matrix times a vector and multiplying two matrices?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT