Signs in the output.

8 ビュー (過去 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 Center および File ExchangeAnnotations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by