Signs in the output.
5 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Mughees Asif
am 20 Feb. 2019
Kommentiert: Mughees Asif
am 22 Feb. 2019
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.
0 Kommentare
Akzeptierte Antwort
per isakson
am 21 Feb. 2019
Bearbeitet: per isakson
am 21 Feb. 2019
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 Kommentare
per isakson
am 21 Feb. 2019
"[...] but that only works for the specified matrix [-1 -2 0]". I disagree. See the addendum to my answer.
Weitere Antworten (1)
Walter Roberson
am 21 Feb. 2019
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
0 Kommentare
Siehe auch
Kategorien
Mehr zu Annotations finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!