필터 지우기
필터 지우기

Movmean skipping NaN in array

조회 수: 3 (최근 30일)
sr9497
sr9497 2022년 3월 27일
댓글: sr9497 2022년 3월 28일
I have an array
x = [20 10 5 NaN]; %and I now use:
movmean([x; x(1, :)], [0 1], 1, 'omitnan', 'Endpoints', 'discard')
ans = 1×4
20 10 5 NaN
to calculate the mean, [15 7.5 5 20].
I would like to get [15 7.5 12.5 NaN] so skip over NaN and calculate the mean of 20 and 5 as well, instead of having NaN being replaced by 20 after using movmean. What is the best way to do this?
  댓글 수: 2
Adam Danz
Adam Danz 2022년 3월 27일
I think you meant to transpose x. It needs to be a column vector in your example.
Adam Danz
Adam Danz 2022년 3월 27일
> I would like to get [15 7.5 12.5 NaN]
Where does the last NaN come from?
What would be the expected value for this: [10 20 NaN 5 NaN NaN 10 20] ?

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

답변 (1개)

Image Analyst
Image Analyst 2022년 3월 27일
Not sure where the 12.5 is coming from but maybe you'd like this:
x = [20, 10, 5, NaN];
kernel = [1,1];
xs = x;
xs(isnan(x)) = 0;
theSum = conv(xs, kernel, 'same')
theSum = 1×4
30 15 5 0
theCount = conv(~isnan(x), kernel, 'same')
theCount = 1×4
2 2 1 0
output = theSum ./ theCount
output = 1×4
15.0000 7.5000 5.0000 NaN
  댓글 수: 5
Image Analyst
Image Analyst 2022년 3월 28일
So now I'm getting confused. Do you have a row vector, or a 2-D matrix? You've shown both.
sr9497
sr9497 2022년 3월 28일
A 2-D matrix, the row vector was a mistake.

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

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by