Filter löschen
Filter löschen

How can I find the average between two points?

3 Ansichten (letzte 30 Tage)
Sandy
Sandy am 20 Jul. 2015
Kommentiert: Sandy am 20 Jul. 2015
So, I basically have a column of data composed of numbers and NaNs. I want my code to go through the column and wherever it finds an NaN, I want it to replace it with the average of the number before and after that NaN. For example:
Input:
23
45
64
NaN
32
Result:
23
45
64
48
32
Please help and thank you so much! :)
  2 Kommentare
David Schubert
David Schubert am 20 Jul. 2015
input = [23; 45; 64; NaN; 32];
output = input;
idx = find(isnan(input));
output(idx) = (input(idx+1)+input(idx-1))/2
However this will only work if the first and the last elements are not NaN.
Sandy
Sandy am 20 Jul. 2015
Thank you!

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 20 Jul. 2015
idx = isnan(Input);
Result = Input;
Result(idx) = (Input(idx-1) + Input(idx+1)) / 2;
This depends upon the NaN not being the first or last entry.
See also John D'Errico's File Exchange contribution inpaint_nans

Weitere Antworten (1)

David Schubert
David Schubert am 20 Jul. 2015
input = [23; 45; 64; NaN; 32];
output = input;
idx = find(isnan(input));
output(idx) = (input(idx+1)+input(idx-1))/2
However this will only work if the first and the last elements are not NaN.

Kategorien

Mehr zu Geometric Transformation and Image Registration finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by