3. Create a 3x5 matrix A full of random real-values and a 5x2 matrix B full of r
ID: 3723474 • Letter: 3
Question
3. Create a 3x5 matrix A full of random real-values and a 5x2 matrix B full of random, real-values. Implement a matrix multiplication routine in a function, C = matrix, mult (A, B) using for-loops. Make the routine's matrices sizes generic. Calculate the matrix C-AB and display the matrices 4. Implement a matrix/vector multiplication routine in a function called, y = matrix-vector, mult (A, X) using only for-loops (there should be only 2 loops used). Make the routine's matrices sizes generic so it will work with a matrix of any size. Create a 5-element column vector x full of random real-values and a 3x5 matrix A full of random real-values. Calculate the matrix y = Ax and display the vectors and the matrices.Explanation / Answer
3) C=matrix_mult(A,B)
Screenshot:-
Function:-
%initialize two random real matrices
A=rand([3 5])
B=rand([5 2])
%function for multiplication
function C=matrix_mult(A,B)
C=zeros(3,2);
%loop for multiplication
for i=1:3
for j=1:2
C(i,j)=0.0;
for k=1:5
C(i,j) = C(i,j)+A(i,k)*B(k,j);
end
end
end
end
%function call
C=matrix_mult(A,B)
---------------------------------------------------------------------------------------------------------------------------
4) Implement matrix/vector multiplicatiion:-
Screenshot:-
%initialize x=column vector and A=matrix
X=rand(5,1)
A=rand([3 5])
%function for multiplication
function Y=matrix_mult(A,X)
sX = size(X);
sA = size(A);
Y=zeros(sA,1);
for i=1:sA
for j=1:sX
p=0;
p=p+X(j)*A(i,j);
end
Y(i)=p;
end
end
%function call
Y=matrix_mult(A,X)
Function:-
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.