make our own signum

2 次查看(过去 30 天)
Dear all,
is there any simple way to transform this javascript logic:
return value < 0 ? -1 : 1
into matlab

采纳的回答

Walter Roberson
Walter Roberson 2021-5-11
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 个)

类别

Help CenterFile Exchange 中查找有关 Time Series Events 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by