Signs in the output.
Afficher commentaires plus anciens
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.
Réponse acceptée
Plus de réponses (1)
Walter Roberson
le 21 Fév 2019
0 votes
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
Catégories
En savoir plus sur Logical dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!