Write a Matlab program to demonstrate common matrix operations: Option 1: Matrix
ID: 3569661 • Letter: W
Question
Write a Matlab program to demonstrate common matrix operations: Option 1: Matrix addition and element by element multiplication (verify that dimensions are the same) Option 2: Matrix multiplication (verify that the number of columns of matrix 1 is equal to the number of rows of matrix2) Option 3: Transpose of a matrix Option 4: Determinant and inverse of a matrix (display message Singular matrix if determinant is less than 1.0 times 10 to the minus tenth power) Option 5: Identity, ones, zeros, random matrices (for identity matrix, create square matrix, only use number of rows (n) to create n by n matrix) See the example output below Match that output. Note: Stay in a while loop until the user enters option 0 If the user enters an option that is an invalid option, display: option: 6 is invalidExplanation / Answer
disp('****welcome to the matrix tutorial*****')
fprintf(' please select 1.matrix addition and element by element multiplication');
fprintf(' 2.matrix multiplication.transpose of a matrix');
fprintf(' 4.inverse of a matrix 5.identity,random andzero matrices');
fprintf(' enter 0 to exit ');
choice=1;
while choice~=0
choice=input(' your choice : ');
switch choice
case 1
a=input('enter the first matrix ');
b=input('enter the second matrix ');
[m1 n1]=size(a);
[m2 n2]=size(b);
if m1~=m2 || n1~=n2
disp('size mismatch')
else
c=a+b;
d=a.*b;
disp('result of addition ')
disp(c)
disp('result of element by element multiplication')
disp(d)
end
case 2
a=input('enter the first matrix ');
b=input('enter the second matrix ');
[m1 n1]=size(a);
[m2 n2]=size(b);
if m2~=n1
disp('size mismatch')
else
c=a*b;
disp('result of multiplication')
disp(c)
end
case 3
a=input('enter the first matrix ');
disp('result of transposition')
disp(a')
case 4
a=input('enter the first matrix ');
if abs(det(a))<=1e-10
disp('determinant is 0')
else
disp('inverse matrix :')
disp(inv(a))
end
case 5
a=input('enter size of the matrix(first row, then column in a matrix format) ')
disp('random matrix ')
rand(a(1),a(2))
disp('zero matrix')
zeros(a(1),a(2))
disp('identity matrix')
eye(a(1))
case 0
break;
otherwise
str=['option ',num2str(choice),' is invalid.'];
disp(str)
end
end
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.