Info

この質問は閉じられています。 編集または回答するには再度開いてください。

simplify a code that takes input matrix and assign negative to one of the values?

1 回表示 (過去 30 日間)
JacobM
JacobM 2016 年 9 月 26 日
閉鎖済み: MATLAB Answer Bot 2021 年 8 月 20 日
this code creates a matrix based on input from the user and then assign negative signs to minimum element in the matrix with first column or second column. Any suggestion how to simplify this code?
prompt0 = 'How many rows in x? ';
x_rows = input(prompt0); x= zeros(x_rows,2);
for m=1:x_rows;
prompt1 = 'What is first column value? ';
x1 = input(prompt1);
if rem(x1,1)~=0 % checking if the input is integer?
error('Input should be an integer');
end
x(m,1)=x1;
x2 = 'What is second column value? ';
x2 = input(prompt2);
if rem(x2,1)~=0
error('Input should be an integer');
end
x(m,2)=x2;
% checking the signs and assign negative sign to
% the minimum value, in case of euality negative
% can be assigned to either values
if (x1-x2)>0
x(m,2)=-x2;
end
if (x2-x1)>=0
x(m,1)=-x1;
end
end

回答 (1 件)

Walter Roberson
Walter Roberson 2016 年 9 月 26 日
No, not in any significant way. Your requirements to prompt at each location and do the error checking do not give any room for vectorization.
There are minor style changes, such as replacing
if (x1-x2)>0
with
if x1>x2

この質問は閉じられています。

Community Treasure Hunt

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

Start Hunting!

Translated by