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.

 Accepted Answer

per isakson
per isakson on 21 Feb 2019
Edited: per isakson on 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 Comments

Thank you for the response but that only works for the specified matrix [-1 -2 0]. I am trying to integrate this function into a programme which has to deal with any arbitrary element 3 row vector e.g. [3 -2 -1] or [2 8 -6].
Create a function that takes as input three parameters: a number, a coordinate name, and a flag. The flag should be initialized to true before the first call. Output is a character vector and the flag.
Rules:
  1. If the input number is 0, then output character vector is empty and do not change the flag
  2. If the input number is negative and the flag is set, then output character vector is negative sign, no space, then abs(value), then the coordinate name, and set the flag to false
  3. If the input number is positive and the flag is set, then output character vector is the value, then the coordinate name, and set the flag to false
  4. If the input number is negative and the flag is not set, then output character vector is space then negative sign then space then abs(value), then the coordinate name, and the flag should be left false
  5. If the input number is positive and the flag is not set, then output character vector is space then positive sign then space then value, then the coordinate name, and the flag should be left false
After processing all three coordinates, if the flag is still true then all three were 0 and you have to decide how you want to output for the case of 0x+0y+0z
This implements the semantics that:
  • all 0 values will be omitted
  • + before the first non-zero will be omitted
  • - before the first non-zero will appear immediately beside the value instead of space between the - and the value. For example, -5x + 3y - 2z instead of - 5x + 3y - 2z
  • + and - will be surrounded by space for interior values. For example -5x + 3y - 2z instead of -5x +3y-2z
Are the coefficients certain to be integers?
"[...] but that only works for the specified matrix [-1 -2 0]". I disagree. See the addendum to my answer.
Thank you sir.

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 21 Feb 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

Community Treasure Hunt

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

Start Hunting!