How can I discretize data by their sign?
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Manling Yang
am 20 Sep. 2022
Kommentiert: Manling Yang
am 20 Sep. 2022
I would like to discretize data by their sign and return the index of 'alternating sign'.
For example if I have a vector:
A = [-2,-1,5,-10,7,3,-9,-12,11];
Their signs are like:
s = sign(A)
[-1,-1,1,-1,1,1,-1,-1,1]
I would like to return:
[1,1,2,3,4,4,5,5,6]
0 Kommentare
Akzeptierte Antwort
Stephen23
am 20 Sep. 2022
A = [-2,-1,5,-10,7,3,-9,-12,11];
B = sign(A);
C = cumsum(logical([1,diff(B)]))
0 Kommentare
Weitere Antworten (1)
Chunru
am 20 Sep. 2022
A = [-2,-1,5,-10,7,3,-9,-12,11];
s = sign(A)
s1 = [1 diff(s)~=0]
y = cumsum(s1)
% [1,1,2,3,4,4,5,5,6]
Siehe auch
Kategorien
Mehr zu MATLAB Compiler 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!