Question

Write your own matlab code to perform matrix multiplication. Recall that to multiply two matrices, the...

Write your own matlab code to perform matrix multiplication. Recall that to multiply two matrices, the inner dimensions must be the same. Write this as a function with input arguments the matrices to be multiplied and output argument the resultant matrix, use "assert" statement(s) to check that the input arguments are valid and print an error message if they are not. Test with A=[1,2,3;4,5,6] and B=[7,8;9,10;11,12] showing results for: (a) A*B, (b) B*A, and (c) A*A.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

%code screenshot

%code to copy

clc
function mul=matrix_mul(A,B)
row_A=size(A,1);
col_A=size(A,2);
row_B=size(B,1);
col_B=size(B,2);
assert(col_A==row_B,'Not Valid Argument.') %assetion
mul=zeros(row_A,col_B);
for i=1:row_A
for j=1:col_B
for k=1:col_A
mul(i,j) = mul(i,j)+ A(i,k) * B(k,j);
end
end
end
  
end


A=[1,2,3;4,5,6];
B=[7,8;9,10;11,12];
result_AB=matrix_mul(A,B) %function result A*B
matlab_result_AB=A*B %matlab result A*B

result_BA=matrix_mul(B,A)
matlab_result_BA=B*A

result_AA=matrix_mul(A,A)

% output screenshot

Add a comment
Know the answer?
Add Answer to:
Write your own matlab code to perform matrix multiplication. Recall that to multiply two matrices, the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please the write code using Matlab 8. Write your own code to perform matrix multiplication. Recall...

    Please the write code using Matlab 8. Write your own code to perform matrix multiplication. Recall that to multiply two matrices, the inner dimensions must be Every element in the resulting C matrix is obtained by: Your code must be a function file and it must check to see if matrix multiplication can be performed on the provided matrices. Test your code with the following matrices: 4 4 2 9 -3 A2 17

  • Write a VBA Sub Program to perform matrices multiplication of matrix A and B using 2D...

    Write a VBA Sub Program to perform matrices multiplication of matrix A and B using 2D arrays. a. Get the number of rows and columns of both matrix A and B from the user, and check if multiplication is possible with matrix A and B. b. If matrices multiplication is possible, input the matrix A and B using arrays and multiply matrix A and B.

  • READ CAREFULLY AND CODE IN C++ Dynamic Programming: Matrix Chain Multiplication Description In this assignment you...

    READ CAREFULLY AND CODE IN C++ Dynamic Programming: Matrix Chain Multiplication Description In this assignment you are asked to implement a dynamic programming algorithm: matrix chain multiplication (chapter 15.2), where the goal is to find the most computationally efficient matrix order when multiplying an arbitrary number of matrices in a row. You can assume that the entire input will be given as integers that can be stored using the standard C++ int type and that matrix sizes will be at...

  • Write a MATLAB program that loads two matrices A and B from the files hw41matA.txt andhw41matB.txt...

    Write a MATLAB program that loads two matrices A and B from the files hw41matA.txt andhw41matB.txt and multiplies them together to obtain matrix C such that the element C(i,j) ofmatrix C is given by: C(i, j) =  ∑A(i, k) ∗ B(k, j) where n = number of columns in A = number of rows in B. Display the matrix C in defaultMATLAB format.
You cannot use the MATLAB matrix multiply or other inbuilt MATLAB functions forarithmetic operations. You must implement it....

  • matrices multiplication in matlab only A&C Consider three matrices: A = [6 -1 12 8 -5...

    matrices multiplication in matlab only A&C Consider three matrices: A = [6 -1 12 8 -5 6] B = [4 0 0.5 1] C = [2 -2 -3 1] Perform all possible multiplications that can be computed between these pairs of matrices. Perform the multiplications in Matlab and check the result by hand (showing work). Use the method in Box PT3.2 to justify why the remaining pairs cannot be multiplied. Perform the comparisons B > C and B < C...

  • Write a c++ program: Many mathematical problems require the addition, subtraction, and multiplication of two matrices. Write an ADT Matrix. You may use the following class definition: const int MAX_RO...

    Write a c++ program: Many mathematical problems require the addition, subtraction, and multiplication of two matrices. Write an ADT Matrix. You may use the following class definition: const int MAX_ROWS = 10; const int MAX_COLS = 10; class MatrixType { public: MatrixType(); void MakeEmpty(); void SetSize(int rowsSize, int colSize); void StoreItem(int item, int row, int col); void Add(MatrixType otherOperand, MatrixType& result); void Sub(MatrixType otherOperand, MatrixType& result); void Mult(MatrixType otherOperand, MatrixType& result); void Print(ofstream& outfile); bool AddSubCompatible(MatrixType otherOperand); bool MultCompatible(MatrixType otherOperand);...

  • MATLAB HELP!!! 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 on...

    MATLAB HELP!!! 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. 5. Recall that if A is an mx n matrix and B is a px q matrix, then the product C-AB is defined if and only if n = p, in which case C is...

  • Write in C code What to do Write a function with the following signature: float* matrix...

    Write in C code What to do Write a function with the following signature: float* matrix multiplication(float* left, float* right, int rows, int shared, int columns); The first two arguments are two pointers to the beginning of two matrices with the dimensions (rows,shared) and (shared, columns) correspondingly. The remaining arguments specify these dimensions. The return value will return a pointer to the very beginning of the result matrix. That said, you need to provide the space for the result matrix...

  • Use MATLAB to write your codes Consider a matrix A with block matrices as follows: A...

    Use MATLAB to write your codes Consider a matrix A with block matrices as follows: A = {A-11 A_12 0 A-22] It can be shown that the inverse of A can be calculated by inverse of submatrices if A11, and A22 are squared matrices: A^-1 = [A_11 A_12 0 A_22] = [A6-1 _11 -A^-1 _11 A_12 A^-1 _22 0 A^-1 _22 Now consider a Matrix A with following submatrices: A11 = identity matrix A22 = identity matrix A12 = [12...

  • Recall that if A is an m times n matrix and B is a p × q matrix

    Recall that if A is an m times 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 times 4 and B is...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT