Info

La pregunta está cerrada. Vuélvala a abrir para editarla o responderla.

Looking for suggestions on cleaning up my program

2 visualizaciones (últimos 30 días)
Michael Purcell
Michael Purcell el 7 de Jul. de 2011
Cerrada: MATLAB Answer Bot el 20 de Ag. de 2021
Hey forum, this is my first post. I am re-learning MATLAB, currently working my way through a textbook from a few semesters ago. The textbook is an older edition and supposedly leaves out a lot of new MATLAB functions- also, I'm still a beginner at programming. I have created a program that I'd like to keep building and editing as I learn more and more stuff. Is there anything in the code that really sticks out as unnecessary or inefficient?
% Matrix Creator
% This script file creates a matrix using for loops and alters them using a switch function.
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')
%After taking user input for the number of columns and rows of Matrix A
%(the user will be prompted to create a matrix B later), the following
%loop is used to collect input for each individual element of the matrix.
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
%At this point, the plan is to prompt the user to do one of many operations
%on the matrix/matrices, which will be done using a switch case command.
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

Respuestas (1)

D
D el 7 de Jul. de 2011
I'm sure there are other more important things, but
if strcmpi(m2, 'y')==1
could be replaced by
if strcmpi(m2, 'y')
Also, if you could insert blank lines in your code, it would be easier to read.

La pregunta está cerrada.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by