How to set multiple values of a threshold?

3 次查看(过去 30 天)
My threshold is 12. If I will get positive values greater than and equal to 12 then I have to set it 1. If I will get negatives values greater than and equal to -12 then I have to set it -1 and all other values that are smaller than -12 and 12 as 0. So how can I set this threshold. In my example valueX1_21 should be -1 but it is 0. How can I get this as -1? Below is my code.
tolX1_4 = [1 1 1 1 1 -1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1];
tolX1_21 = [ -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 0 -1 -1 -1 -1 -1 0 -1 -1 -1 -1 -1 -1];
tolX1_22 = [ 0 0 0 0 0 0 0 0 0 -1 -1 0 0 -1 0 0 -1 -1 0 0 0 0 0 0];
SumX1_4 = sum(tolX1_4)
SumX1_21 = sum(tolX1_21)
SumX1_22 = sum(tolX1_22)
threshold = 12;
valueX1_4 = double(SumX1_4 >= threshold)
valueX1_21 = double(SumX1_21 >= threshold)
valueX1_22 = double(SumX1_22 >= threshold)

采纳的回答

Alan Stevens
Alan Stevens 2023-3-17
编辑:Alan Stevens 2023-3-17
Here's one way:
tolX1_4 = [1 1 1 1 1 -1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1];
tolX1_21 = [ -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 0 -1 -1 -1 -1 -1 0 -1 -1 -1 -1 -1 -1];
tolX1_22 = [ 0 0 0 0 0 0 0 0 0 -1 -1 0 0 -1 0 0 -1 -1 0 0 0 0 0 0];
threshold = 12;
value = @(S) (sign(S)*S>=threshold)*sign(S);
SumX1_4 = sum(tolX1_4)
SumX1_4 = 20
SumX1_21 = sum(tolX1_21)
SumX1_21 = -22
SumX1_22 = sum(tolX1_22)
SumX1_22 = -5
valueX1_4 = value(SumX1_4)
valueX1_4 = 1
valueX1_21 = value(SumX1_21)
valueX1_21 = -1
valueX1_22 = value(SumX1_22)
valueX1_22 = 0
or define value as
value = @(S) (abs(S)>=threshold)*sign(S);

更多回答(0 个)

Community Treasure Hunt

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

Start Hunting!

Translated by