Question

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. You must also check that the dimensions ofmatrices A and B are valid for such a multiplication. You may use length() or size() to check thedimensions. Use minimum number of loops and operations possible.

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

There is a pre defined function "dlmread".This reads the whole file into matrix it automatically detects the deimeter.we also have another function
M = dlmread(filename,delimiter) where we can specify the delimiter.
M = dlmread('myfile.txt') to read matrix from file


%matlab code

% read matrix A from file
A = dlmread('hw41matA.txt',' ')
A = dlmread('myfile.txt')

% read matrix B from file
B = dlmread('hw41matB.txt',' ')
B = dlmread('myfile.txt')


[rowx, columnx] = size(A);
[rowy, columny] = size(B);
C = zeros(rowx, columny);
for i = 1 : rowx
   for j = 1 : columny
       sum = 0;
       for k = 1 : columnx
           sum = sum + A(i, k) * B(k, j);
       end
       C(i, j) = sum;
   end
end
%display product matrix
disp(C);

Add a comment
Know the answer?
Add Answer to:
Write a MATLAB program that loads two matrices A and B from the files hw41matA.txt andhw41matB.txt...
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
  • 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.

  • 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.

  • C++ must use header files and implementation files as separate files. I’ll need a header file,...

    C++ must use header files and implementation files as separate files. I’ll need a header file, implementation file and the main program file at a minimum. Compress these files into one compressed file. Make sure you have adequate documentation. We like to manipulate some matrix operations. Design and implement a class named matrixMagic that can store a matrix of any size. 1. Overload the addition, subtraction, and multiplication operations. 2. Overload the extraction (>>) and insertion (<<) operators to read...

  • Write a method to multiply two matrices. The header of the method is: public static double[][]...

    Write a method to multiply two matrices. The header of the method is: public static double[][] multiplyMatrix(double[][] a, double[][] b) To multiply matrix a by matrix b, the number of columns in a must be the same as the number of rows in b, and the two matrices must have elements of the same or compatible types. Let c be the result of the multiplication. Assume the column size of matrix a is n. Each element is For example, for...

  • Problem 1 Write your code in the file MatrixOps.java. . Consider the following definitions from matrix...

    Problem 1 Write your code in the file MatrixOps.java. . Consider the following definitions from matrix algebra: A vector is a one-dimensional set of numbers, such as [42 9 20]. The dot product of two equal-length vectors A and B is computed by multiplying the first entry of A by the first entry of B, the second entry of A by the second entry of B, etc., and then summing these products. For example, the dot product of [42 9...

  • 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

  • MUST BE PROCEDURAL CODE. Write a program in C++ that reads two matrices and: 1) adds...

    MUST BE PROCEDURAL CODE. Write a program in C++ that reads two matrices and: 1) adds them (if compatible) and prints their sum, 2) subtracts them (if compatible) and prints their difference, and 3) multiplies them (if compatible) and prints their product. Prompt the user for the names of two matrix input files (see format below) and place the output in a file of the user’s choosing. The format of the input files should contain the number of rows, followed...

  • Please help me out with this assignment. Please read the requirements carefully. And in the main...

    Please help me out with this assignment. Please read the requirements carefully. And in the main function please cout the matrix done by different operations! Thanks a lot! For this homework exercise you will be exploring the implementation of matrix multiplication using C++ There are third party libraries that provide matrix multiplication, but for this homework you will be implementing your own class object and overloading some C+ operators to achieve matrix multiplication. 1. 10pts] Create a custom class called...

  • 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);...

  • You have two Matrices, A and B. Assume that they are the correct dimensions (i.e. Both...

    You have two Matrices, A and B. Assume that they are the correct dimensions (i.e. Both rows and columns are equal). Write the MATLAB code to do the following. a) Add A and B b) Subtract B from A c) Multiply A and B d) Divide B by A e) A Cubed

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