필터 지우기
필터 지우기

How can I find the average between two points?

조회 수: 3 (최근 30일)
Sandy
Sandy 2015년 7월 20일
댓글: Sandy 2015년 7월 20일
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
David Schubert
David Schubert 2015년 7월 20일
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 2015년 7월 20일
Thank you!

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
Walter Roberson 2015년 7월 20일
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

추가 답변 (1개)

David Schubert
David Schubert 2015년 7월 20일
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.

카테고리

Help CenterFile Exchange에서 Geometric Transformation and Image Registration에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by