필터 지우기
필터 지우기

Bachelor Thesis Help! Dealing with NaN when calculating absolute difference

조회 수: 4 (최근 30일)
Dear reader,
I'm working on some gait data for my bachelor thesis. I stubble across the following problem:
I wanna calculate the absolute difference between two consecutive values. When the value is NaN, I want to skip this value and calculate the absolute difference of the next two values. The new variable must not consist any gaps in between values, so it has to be one vector without zeros.
I wrote the following:
for i = start : stop-1; j = i - start+1;
if ContactTimeL(i,1)==NaN;
i = i+1;
else
ADContactTimeL(j,1) = abs(PContactTimeL(j+1,1) - PContactTimeL(j,1));
end
end
The problem is that every time the loops start again, i is ascending with from the next original value instead of i = i +1.
Does anyone know how I can fix this problem?
Thanks in advance!

채택된 답변

Mark Stone
Mark Stone 2015년 5월 19일
편집: Mark Stone 2015년 5월 19일
I think this will give you the idea. You can fix it up to suit your needs.
First of all, use isnan(y) to determine whether y is NaN. Even if y = NaN, y == NaN is false (don't blame me, I didn't make the rules).
Assume you have your data in the vector x.
diff_x = abs(diff(x)); % differences data and takes abs, but you will be left with NaN values
diff_x = diff_x(~isnan(diff_x)); % removes NaN values
Example:
>> x = [1 2 NaN 3 4 NaN NaN 5 NaN 4 2 NaN 7 2 NaN 2 NaN NaN NaN 5]
x =
1 2 NaN 3 4 NaN NaN 5 NaN 4 2 NaN 7 2 NaN 2 NaN NaN NaN 5
>> diff_x = diff(x)
diff_x =
1 NaN NaN 1 NaN NaN NaN NaN NaN 2 NaN NaN 5 NaN NaN NaN NaN NaN NaN
>> diff_x = diff_x(~isnan(diff_x))
diff_x =
1 1 2 5

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Solver Outputs and Iterative Display에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by