count number of shifts in data based on conditions
1 view (last 30 days)
Show older comments
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.
0 Comments
Accepted Answer
Thorsten
on 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);
0 Comments
More Answers (1)
See Also
Categories
Find more on Logical in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!