Effacer les filtres
Effacer les filtres

Bachelor Thesis Help! Dealing with NaN when calculating absolute difference

4 vues (au cours des 30 derniers jours)
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!

Réponse acceptée

Mark Stone
Mark Stone le 19 Mai 2015
Modifié(e) : Mark Stone le 19 Mai 2015
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

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by