Help in writing a a code for an array based on comdition.
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Haya Ali
am 17 Mär. 2023
Kommentiert: Walter Roberson
am 17 Mär. 2023
I have an array A. A1= [-0.0082 -0.0409 -0.0175 -0.0853 0.0217 -0.2556 -0.0302 0 ]
From A array I want to replace negative numbers with -1, positive numbers with 1 and zero will remain zero . Like this :
A= [-1 -1 -1 -1 1 -1 -1 0]
Then I want to take there sum and if there sum is smaller than threshold then value will be set to zero. If equals and greater than threshold than value will be 1. Threshold is 12.
How to write code for this?
0 Kommentare
Akzeptierte Antwort
Torsten
am 17 Mär. 2023
A1 = [-0.0082 -0.0409 -0.0175 -0.0853 0.0217 -0.2556 -0.0302]
A = -(A1<0) + (A1>0)
s = sum(A)
threshold = 12;
value = double(s >= threshold)
4 Kommentare
Walter Roberson
am 17 Mär. 2023
0 and -0 display as 0 not as 0.0000 or -0.0000. So your values are not 0 and -0
Give the command
format long g
and then display your values again
Weitere Antworten (1)
Siehe auch
Kategorien
Mehr zu Environment and Settings finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!