필터 지우기
필터 지우기

count number of shifts in data based on conditions

조회 수: 1 (최근 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 CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by