How can I discretize data by their sign?

조회 수: 1 (최근 30일)
Manling Yang
Manling Yang 2022년 9월 20일
댓글: Manling Yang 2022년 9월 20일
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]

채택된 답변

Stephen23
Stephen23 2022년 9월 20일
A = [-2,-1,5,-10,7,3,-9,-12,11];
B = sign(A);
C = cumsum(logical([1,diff(B)]))
C = 1×9
1 1 2 3 4 4 5 5 6

추가 답변 (1개)

Chunru
Chunru 2022년 9월 20일
A = [-2,-1,5,-10,7,3,-9,-12,11];
s = sign(A)
s = 1×9
-1 -1 1 -1 1 1 -1 -1 1
s1 = [1 diff(s)~=0]
s1 = 1×9
1 0 1 1 1 0 1 0 1
y = cumsum(s1)
y = 1×9
1 1 2 3 4 4 5 5 6
% [1,1,2,3,4,4,5,5,6]

카테고리

Help CenterFile Exchange에서 MATLAB Compiler에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by