Bachelor Thesis Help! Dealing with NaN when calculating absolute difference

3 ビュー (過去 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 Center および File ExchangeResizing and Reshaping Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by