# make our own signum

4 views (last 30 days)
Soveatin Kuntur on 11 May 2021
Answered: Walter Roberson on 11 May 2021
Dear all,
is there any simple way to transform this javascript logic:
return value < 0 ? -1 : 1
into matlab
##### 0 CommentsShowHide -1 older comments

Sign in to comment.

### Accepted Answer

Walter Roberson on 11 May 2021
Under the interpretation that nan values are not less than 0 (they are not greater than 0 either)
output = (value < 0) .* -2 + 1;
output(isnan(value)) = 1;
or
output = (value < 0) .* -1 + (value >= 0) .* 1
output(isnan(value)) = 1;
or
output = (value >= 0) - (value < 0);
output(isnan(value)) = 1;
or
output = sign(value) + (value == 0);
output(isnan(value)) = 1;
If you are certain that the values will never be NaN then you can simplify the code.
##### 0 CommentsShowHide -1 older comments

Sign in to comment.

### Categories

Find more on Data Type Identification in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by