USE MATLAB 3. Create an m-function that will perform Matrix addition and Matrix
ID: 2249168 • Letter: U
Question
USE MATLAB
3. Create an m-function that will perform Matrix addition and Matrix Multiplication Here are the required conditions. (1) Use 4 numbers as arguments. First and second numbers will become row and column number of the first Matrix. The become row and column numbers of the second Matrix f the dimension of the first Matrix is not equal to the dimension of the second Matrix, the computer WILLNOT perform the addition. third and fourth numbers will 3)ALSQ, If the column number of the first Matrix is not equal to the rovw number of the second Matrix, the computer WILLNOT perform the multiplication. (4) Once the dimension conditions are cleared, the user should be prompted to type in individual entries of the two Matrices.Explanation / Answer
% Clear screen, clear previous variables and closes all figures
clc; close all; clear
% Avoid empty lines
format compact
% Define matrices, for example these
A = [2 1 4 1 2; 1 0 1 2 -1; 2 3 -1 0 -2]
B = [-2 -1 2; 0 2 1; -1 1 4; 3 0 1; 2 1 2]
% The size of each matrix is considered for these calculations
[r1 c1] = size(A);
[r2 c2] = size(B);
% prevent unappropriate matrix size
if c1 ~= r2
disp ('*** not able to multiply matrices ***')
end
% Main code
% change every row of matrix A
for i = 1 : r1
% change every column of matrix B
for j = 1 : c2
% Reset every new element of the final result
s = 0;
% change every column of matrix A and row of matrix B
for k = 1 : c1
% check every element to take into account
A(i,k)
B(k,j)
% check the addition in the iteration
s = s + A(i,k) * B(k,j);
end
% maintain the total of the appropriate element
% lead to the final matrix
C(i,j) = s
end
end
% Check with our result with a multiplication by Matlab
A*B
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.