How to set multiple values of a threshold?

7 ビュー (過去 30 日間)
Haya Ali
Haya Ali 2023 年 3 月 17 日
編集済み: Alan Stevens 2023 年 3 月 17 日
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 件)

カテゴリ

Help Center および File ExchangeAssembly についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by