count number of shifts in data based on conditions
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Thorsten
2017-3-14
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 个评论
更多回答(1 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!