Write a MATIAB script that will add four matrices input by t
Solution
MATLAB CODE
clc; clear all; % Clearing the commadd window and workspace
% getting the matrices from the user
A = input(\'Enter a matrix of the form [a11,a12,...,a1n;a21....a2n;....;an1,...,ann]\ \');
B = input(\'Enter an another matrix of similar form\ \');
C = 0;
try % To catch the error in matrix addition
C = A+B;
% If no erros print the result
fprintf(\'The sum\');
disp(C);
catch ME % In the case of an error
try % to catch the error in the column addtion
A(:,1)+B(:,1);
catch ME1 % In the case of an errorprint the message
warning(\' The number of rows of the matrices must match\');
end
try % to catch the error in the row addition
A(1,:)+B(1,:);
catch ME2 % in the case of an error print the message
warning(\' The number of columns of the matrices must match\');
end
end
OUTPUTS
Enter a matrix of the form [a11,a12,...,a1n;a21....a2n;....;an1,...,ann]
[1,2,3;1,2,3]
Enter an another matrix of similar form
[1,2,3,4;1,2,3,4]
Warning: The number of columns of the matrices must match
Enter a matrix of the form [a11,a12,...,a1n;a21....a2n;....;an1,...,ann]
[1,2,3;1,2,3]
Enter an another matrix of similar form
[1,2,3;1,2,3;1,2,3]
Warning: The number of rows of the matrices must match
Enter a matrix of the form [a11,a12,...,a1n;a21....a2n;....;an1,...,ann]
[1,2,3;1,2,3;1,2,3]
Enter an another matrix of similar form
[1,2,3;1,2,3;1,2,3]
The sum 2 4 6
2 4 6
2 4 6
