Detect change between two values in an array

Is there any function in Matlab that allows you to read through a column in an array and see if a previous value is the same as a current value? I know there is the "detect change" function block in Simulink and was looking to do something similar with code, however.

 채택된 답변

Sean de Wolski
Sean de Wolski 2013년 10월 1일
편집: Sean de Wolski 2013년 10월 1일

0 개 추천

x = [1 1 2 3 4 4 5 6 6 6 6 7];
diff(x)==0 %?

댓글 수: 4

Muneer
Muneer 2013년 10월 1일
That method actually finds the difference between the numbers. I don't want to subtract them, just register a change. For example if my data stream is [ 0 0 4 5 7 3 3 87 2 5 1 1], then something that registers a difference would compare the second value to the first, the third to the second, etc and for every change it would record a 1 in a new array or a 0 for no change. so the stream above would become [0 1 1 1 1 0 1 1 1 1 0]. This is what the detect change block does. I just want to do the same thing with code.
Thanks
Then what I have above is exactly what you want, it's just the opposite, where I kept differences you want to mark them.
x = [ 0 0 4 5 7 3 3 87 2 5 1 1]
logical(diff(x))
Muneer
Muneer 2013년 10월 3일
Thanks for your help, that did the trick
Brandon Leonard
Brandon Leonard 2018년 4월 16일
편집: Brandon Leonard 2018년 4월 16일
Sean de Wolski would this same approach work for two values from data collected (e.g. day = 1; night = 2) to locate where this change occurs in continuous data if data was stored in a timeseries? Could it be logged as a event(e.g. tsdata.event.EventData = logical(diff(x)) somehow?

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

추가 답변 (1개)

Image Analyst
Image Analyst 2013년 10월 1일

1 개 추천

Try this:
x=[ 0 0 4 5 7 3 3 87 2 5 1 1]
changedIndexes = diff(x)~=0

댓글 수: 6

Muneer
Muneer 2013년 10월 3일
Thanks
Could you extend this to an cell array, please? Thank you!
Could you extend this to an cell array, please? Thank you!
Use cell2mat() on your cell array first.
what if the table has nan values?
Table variables can be nan, just like double variables. Actually a column of a table has to be a predetermined type, like double, char, int32, or whatever. So if a column is double, then it's allowable for a row in that double column to have a value of "nan".

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

카테고리

도움말 센터File Exchange에서 Cell Arrays에 대해 자세히 알아보기

질문:

2013년 10월 1일

댓글:

2018년 8월 25일

Community Treasure Hunt

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

Start Hunting!

Translated by