Info

This question is closed. Reopen it to edit or answer.

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

6 views (last 30 days)
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

Answers (1)

Walter Roberson
Walter Roberson on 26 Sep 2016
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

This question is closed.

Community Treasure Hunt

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

Start Hunting!