Signs in the output.

조회 수: 7 (최근 30일)
Mughees Asif
Mughees Asif 2019년 2월 20일
댓글: Mughees Asif 2019년 2월 22일
h = [-1 -2 0]
j=sqrt(sum(h.^2));
%Number conversion into string format
k=num2str(h(1));
l=num2str(h(2));
m=num2str(h(3));
%Error message; distance input must be a positive real number
n=input('Enter the perpendicular distance from the origin: ');
if n<=0
disp('The distance cannot be zero or negative. Try again.')
else
o=['The equation of the plane at a perpendicular distance' ...
' of ', num2str(n),' from the origin O, is ', k,'x + ', ...
l,'y + ', m,'z = ', num2str(j*n),'.'];
disp(o)
end
When I input a distance of 2, the result is displayed as:
-1x + -2y + 0z = 4.4721.
Is there any way of displaying the output simply as:
-x - 2y = 4.4721.
where, the function automatically picks up the sign in the matrix and uses that instead of displaying both + and - . When the value is one it only displays the variable and when zero, it omits the variable from the output?
Thank you.

채택된 답변

per isakson
per isakson 2019년 2월 21일
편집: per isakson 2019년 2월 21일
This script nearly makes it
%%
h = [-1 -2 0];
v = {'x','y','z'};
j = sqrt(sum(h.^2));
z = 0;
while z <= 3
z = z + 1;
n = input('Enter the perpendicular distance from the origin: ');
if n<=0
disp('The distance cannot be zero or negative. Try again.')
else
len = length( h );
str = cell(1,len+1);
for jj = 1 : len
if sign( h(jj) ) >= 0
if jj == 1
sgn = '';
else
sgn = '+';
end
else
sgn = '-';
end
if abs(h(jj))>=2
str{jj} = sprintf( '%c %d%c ', sgn, abs(h(jj)), v{jj} );
elseif abs(h(jj))==1
str{jj} = sprintf( '%c %c ', sgn, v{jj} );
else
str{jj} = '';
end
end
str{len+1} = sprintf( '= %.4f.', j*n );
out = strjoin( str, '' ) %#ok<NOPTS>
break
end
end
It outputs
Enter the perpendicular distance from the origin: 2
out =
'- x - 2y = 4.4721.'
In response to a comment. In what respect doesn't it work? Now the only problem I see is the leading space
Another two tests
>> cssm([3 -2 -1])
Enter the perpendicular distance from the origin: 2
ans =
' 3x - 2y - z = 7.4833.'
>> cssm( [2 8 -6] )
Enter the perpendicular distance from the origin: 2
ans =
' 2x + 8y - 6z = 20.3961.'
>>
So far so good. However,
>> cssm( [0 8 -6] )
Enter the perpendicular distance from the origin: 2
ans =
'+ 8y - 6z = 20.0000.'
The leading "+" isn't wanted. And [0,0,0]
>> cssm( [0 0 0 ])
Enter the perpendicular distance from the origin: 2
ans =
'= 0.0000.'
where
function out = cssm( h )
%%
v = {'x','y','z'};
j = sqrt(sum(h.^2));
z = 0;
while z <= 3
z = z + 1;
n = input('Enter the perpendicular distance from the origin: ');
if n<=0
disp('The distance cannot be zero or negative. Try again.')
else
len = length( h );
str = cell(1,len+1);
for jj = 1 : len
if sign( h(jj) ) >= 0
if jj == 1
sgn = '';
else
sgn = '+';
end
else
sgn = '-';
end
if abs(h(jj))>=2
str{jj} = sprintf( '%c %d%c ', sgn, abs(h(jj)), v{jj} );
elseif abs(h(jj))==1
str{jj} = sprintf( '%c %c ', sgn, v{jj} );
else
str{jj} = '';
end
end
str{len+1} = sprintf( '= %.4f.', j*n );
out = strjoin( str, '' );
break
end
end
end
  댓글 수: 5
per isakson
per isakson 2019년 2월 21일
"[...] but that only works for the specified matrix [-1 -2 0]". I disagree. See the addendum to my answer.
Mughees Asif
Mughees Asif 2019년 2월 22일
Thank you sir.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Walter Roberson
Walter Roberson 2019년 2월 21일
Not using disp.
If you use fprintf then if you use a plus sign between the % and the numeric width specification then the sign of the value will be output even if the data is positive
%+.3g
for example

카테고리

Help CenterFile Exchange에서 Annotations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by