count number of shifts in data based on conditions

1 vue (au cours des 30 derniers jours)
JE
JE le 13 Mar 2017
Réponse apportée : JE le 15 Mar 2017
I am trying to write an if statement that counts how many times a shift in data occurs within a certain range. A shift is identified as a change in data by a value of 1 from the last data point. I would like to create a vector that is the same length as the range I am referring to by indexing, that will contain either 1's or 0's. Once the vector is created I will use another function to count the number of shifts. I know how to do this in Excel but I am new to Matlab so I appreciate your help. I've tried to go through the help docs and community but have not had much success.
My results are repeating
j = 1
j = 1
j = 0 . . .
if true
% code
%Here is my code:
for CurrentGear = TimeStartIdx:TimeEndIdx
if CurrentGear < CurrentGear-1
j = 1
elseif CurrentGear > CurrentGear -1
j = 1
else CurrentGear = CurrentGear -1
j =0
end
TimeStartIdx and TimeEndIdx are the range I found by indexing.
Thanks.

Réponse acceptée

Thorsten
Thorsten le 14 Mar 2017
If x is the vector of your data, you can get the vector of shifts by 1 as
dx = diff(x) == 1;
And to count the number of 1's
N = nnz(dx);

Plus de réponses (1)

JE
JE le 15 Mar 2017
Thank you, that's much simpler than a for loop with if statement.

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by