Detect change between two values in an array

81 visualizzazioni (ultimi 30 giorni)
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.

Risposta accettata

Sean de Wolski
Sean de Wolski il 1 Ott 2013
Modificato: Sean de Wolski il 1 Ott 2013
x = [1 1 2 3 4 4 5 6 6 6 6 7];
diff(x)==0 %?
  4 Commenti
Muneer
Muneer il 3 Ott 2013
Thanks for your help, that did the trick
Brandon Leonard
Brandon Leonard il 16 Apr 2018
Modificato: Brandon Leonard il 16 Apr 2018
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?

Accedi per commentare.

Più risposte (1)

Image Analyst
Image Analyst il 1 Ott 2013
Try this:
x=[ 0 0 4 5 7 3 3 87 2 5 1 1]
changedIndexes = diff(x)~=0
  6 Commenti
Nikolaos Vasilas
Nikolaos Vasilas il 25 Ago 2018
what if the table has nan values?
Image Analyst
Image Analyst il 25 Ago 2018
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".

Accedi per commentare.

Categorie

Scopri di più su Time Series Collections in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by