How to detect a data value change in matlab

looking for yur help one more time.
I have data that is constanly hanging, here I share one portion of it
I'm trying to create a variable/falg that helps me to identify when there is a change, somethinng like this:
DATA variable/flag
0 -----> matlab code -----> 0
0 0
0 0
0.1224 1
0.1219 1
0.1199 1
0.1199 0
0.1199 0
0.1175 1
0.1175 0
0.1175 0
0.1175 0
0.1300 1
0.1300 0
0.1200 1
DATA values can change any time, keep same value for different periods of time, and either increase, decrease or change to positive or negative side.
I don't have much experience with mat lab so any feedback will be highly appreciated.
thanks

 채택된 답변

Voss
Voss 2024년 6월 11일
편집: Voss 2024년 6월 11일
DATA = [
0
0
0
0.1224
0.1219
0.1199
0.1199
0.1199
0.1175
0.1175
0.1175
0.1175
0.1300
0.1300
0.1200
];
is_change = [false; diff(DATA) ~= 0]
is_change = 15x1 logical array
0 0 0 1 1 1 0 0 1 0 0 0 1 0 1
% a table for viewing is_change alongside DATA
result = table(DATA,is_change)
result = 15x2 table
DATA is_change ______ _________ 0 false 0 false 0 false 0.1224 true 0.1219 true 0.1199 true 0.1199 false 0.1199 false 0.1175 true 0.1175 false 0.1175 false 0.1175 false 0.13 true 0.13 false 0.12 true

댓글 수: 6

A-Rod
A-Rod 2024년 6월 12일
awsome, this works and it helps me a lot, thankyou for taking time to share.
Voss
Voss 2024년 6월 12일
You're welcome!
A-Rod
A-Rod 2024년 6월 12일
Voss.
I ran into a problem when my data goes negative, I need to make my flag true when data values are negative even if they don't change, like this:
DATA variable/flag
0 -----> matlab code -----> 0
0 0
0 0
0.1224 1
0.1219 1
0.1199 1
0.1199 0
0.1199 0
0.1175 1
0.1175 0
0.1175 0
0.1175 0
0.1300 1
0.1300 0
0.1200 1
-0.0007 1
-0.0007 1
-0.0007 1
-0.0082 1
-0.0081 1
0.1200 1
0.1200 0
];
I tried to complement the logical function you shared:
is_change = [false; diff(DATA) ~= 0 & true; DATA<0]
but is not working as I expected since the output is a vector 2 times the length of DATA.
is there a way to combine it and get just one vector with the length of DATA but keeping both logical conditions? like the column like the one shown in desired output
DATA is_change
output desired output
0 false false
0 false false
0 false false
0.1224 true true
0.1219 true true
0.1199 true true
0.1199 false false
0.1199 false false
0.1175 true false
0.1175 false false
0.1175 false false
0.1175 false false
0.13 true true
0.13 false false
0.12 true true
-0007 false true
-0007 false true
-0007 false true
-0082 false true
-0081 false true
0.12 true true
0.12 false true
false
false
false
false
false
false
false
false
false
false
false
false
false
false
false
true
true
true
true
true
false
false
I thank you in advance and I stay tuned for any update
Voss
Voss 2024년 6월 12일
편집: Voss 2024년 6월 12일
DATA = [
0
0
0
0.1224
0.1219
0.1199
0.1199
0.1199
0.1175
0.1175
0.1175
0.1175
0.1300
0.1300
0.1200
-0007
-0007
-0007
-0082
-0081
0.12
0.12
];
is_change = [false; diff(DATA) ~= 0] | DATA<0
is_change = 22x1 logical array
0 0 0 1 1 1 0 0 1 0 0 0 1 0 1 1 1 1 1 1 1 0
% a table for viewing is_change alongside DATA
result = table(DATA,is_change);
disp(result)
DATA is_change ______ _________ 0 false 0 false 0 false 0.1224 true 0.1219 true 0.1199 true 0.1199 false 0.1199 false 0.1175 true 0.1175 false 0.1175 false 0.1175 false 0.13 true 0.13 false 0.12 true -7 true -7 true -7 true -82 true -81 true 0.12 true 0.12 false
A-Rod
A-Rod 2024년 6월 13일
thank you so much!
Voss
Voss 2024년 6월 13일
You're welcome!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

제품

질문:

2024년 6월 11일

댓글:

2024년 6월 13일

Community Treasure Hunt

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

Start Hunting!

Translated by