How can I stop my program and ask for the wish operation?

1 回表示 (過去 30 日間)
Johan De Gracia
Johan De Gracia 2020 年 11 月 29 日
編集済み: Setsuna Yuuki. 2020 年 12 月 2 日
Hello everyone, I have this program who ask for rows of matrix and columns. My problem is that once I have already asked for columns and rows of matrix A and B I need to ask what operation the person wants to do. For example. I need to ask "Selesct pointwise operation of matrices A and B: 1: Multiplication 2:Division 3:Exponential.
However my program realize the mutiplication and division by itself and I need to stop it and ask first.
disp(' ');
switch_1=1;
while switch_1==1
rows_matrix_A=input('Enter the rows of Matrix A => ');
columns_matrix_A=input('Enter the columns of Matrix A => ');
rows_matrix_B=input('Enter the rows of Matrix B => ');
columns_matrix_B=input('Enter the columns of Matrix B => ');
disp(' ')
if columns_matrix_A==rows_matrix_B
for i=1:rows_matrix_A
for j=1:columns_matrix_A
A(i,j)=input('Enter element of matrix A => ');
end
end
disp(' ')
for i=1:rows_matrix_B
for j=1:columns_matrix_B
B(i,j)=input('Enter element of matrix B => ');
end
end
disp(' ')
disp('Matrix A: ')
disp(A)
disp(' ')
disp('Matrix B: ')
disp(B)
C=A*B;
disp(' ')
disp('Matrix C: ')
disp(C)
D=A/B;
disp(' ')
disp('Matrix C: ')
disp(D)
else
disp('Error, Standard multiplication cannot be perfomed! ')
disp(' ')
end
switch_1=input('DO you want to run the program again? 1=YES 2=NO ');
disp(' ')
end
  3 件のコメント
Johan De Gracia
Johan De Gracia 2020 年 12 月 2 日
???
Walter Roberson
Walter Roberson 2020 年 12 月 2 日
https://www.mathworks.com/help/matlab/ref/menu.html

サインインしてコメントする。

回答 (1 件)

Setsuna Yuuki.
Setsuna Yuuki. 2020 年 12 月 2 日
編集済み: Setsuna Yuuki. 2020 年 12 月 2 日
You can use a if
umi = '1 = A*B 2 = A/B ';
opc = input(umi); %input (multiply or divide?)
disp(' ')
disp('Matrix A: ')
disp(A)
disp(' ')
disp('Matrix B: ')
disp(B)
if (opc == 1)
C=A.*B;
disp(' ')
disp('Matrix C: ')
disp(C)
elseif (opc == 2)
D=A./B;
disp(' ')
disp('Matrix C: ')
disp(D)
end

カテゴリ

Help Center および File ExchangeWrite Unit Tests についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by