Write a MATLAB program that loads two matrices A and B from the files hw41matA.t
ID: 3830311 • Letter: W
Question
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.
Explanation / Answer
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);
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.