clc;
fprintf('The following program will ask for matrix dimensions and then prompt\n you to enter the matrix elements, by row then by column.\n');
ma=input('Enter the number of rows you would like matrix A to have: ');
na=input('Enter the number of columns you would like matrix A to have: ');
A=zeros(ma,na);
fprintf('\n')
for r=1:ma
for c=1:na
fprintf('\tEnter row %i, column %i:',r,c)
A(r,c)=input('');
end
fprintf('\n')
end
A
fprintf('\n')
m2=input('Would you like to work with another matrix? (y/n):','s');
if strcmpi(m2, 'y')==1
mb=input('Enter the number of rows you would like the second matrix to have: ');
nb=input('Enter the number of columns you would like the second matrix to have: ');
B=zeros(mb,nb);
fprintf('\n')
for r=1:mb
for c=1:nb
fprintf('\tEnter row %i, column %i:',r,c)
B(r,c)=input('');
end
fprintf('\n')
end
B
else
disp('Now to work on the matrix...')
end
fprintf('\nAt this point, you may choose from the following operations to perform')
fprintf('on the matrix:\n* = Matrix must be square\n** = Matrix dimensions')
fprintf('must fit\n\t1 - transpose\n\t2 - determinant**\n\t3 - inverse**\n\t')
fprintf('4 - square**\n\t5 - square each element\n')
d=input('Enter choice: ');
cm=input('Enter which matrix(A or B) to perform operation on: ','s');
switch cm
case {'a','A'}
switch d
case 1
C=A';
case 2
if ma==na
C=det(A);
else
disp('Error, the matrix is not square')
end
case 3
if ma==na
C=inv(A);
else
disp('Error, the matrix is not square')
end
case 4
if ma==na
C=A^2;
else
disp('Error, the matrix is not square')
end
case 5
C=A.^2;
end
case {'b','B'}
switch d
case 1
C=B';
case 2
if mb==nb
C=det(B);
else
disp('Error, the matrix is not square')
end
case 3
if mb==nb
C=inv(B);
else
disp('Error, the matrix is not square')
end
case 4
if mb==nb
C=B^2;
else
disp('Error, the matrix is not square')
end
case 5
C=B.^2;
end
end
C
0 Comments
Sign in to comment.