Write a MATIAB script that will add four matrices input by t

Write a MATIAB script that will add four matrices input by the user (at the keyboard). If all the matrices cannot be added together, inform the user of the first matrix that caused the error (i.e. the first matrix that is not the same size as the previous matrices) using multiple try/catch structures.

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

 Write a MATIAB script that will add four matrices input by the user (at the keyboard). If all the matrices cannot be added together, inform the user of the fir

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site