フィルターのクリア

count number of shifts in data based on conditions

2 ビュー (過去 30 日間)
JE
JE 2017 年 3 月 13 日
回答済み: JE 2017 年 3 月 15 日
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.

採用された回答

Thorsten
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);

その他の回答 (1 件)

JE
JE 2017 年 3 月 15 日
Thank you, that's much simpler than a for loop with if statement.

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by