Movmean skipping NaN in array

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 Kommentare

Adam Danz
Adam Danz am 27 Mär. 2022
I think you meant to transpose x. It needs to be a column vector in your example.
Adam Danz
Adam Danz am 27 Mär. 2022
> 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] ?

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Image Analyst
Image Analyst am 27 Mär. 2022

0 Stimmen

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 Kommentare

Adam Danz
Adam Danz am 27 Mär. 2022
I believe the 12.5 comes from averaging 5 and 20 in [20,10,5,NaN,20] while ignoring the NaN but that left me wondering where the NaN came from in the expected results.
Image Analyst
Image Analyst am 28 Mär. 2022
So what's the rule? Always tack on the first element to the end? Or just tack it on if the last element is nan?
What's the use case here? What's the real world situation we need to model? I'mm just not sure why this special endpoint handling is needed, or if it even is needed.
What if there are nan's of various lengths sprinkled throughout the array? Like what if the array is 10000 long but there is a stretch of 10 nan's in there? Do we just want to remove all nan's in the array and take pairs of what's left? If not, then what would go into that stretch of 10 nan's? Do we "fix them" with an interpolated value like you'd get with interp1() or regionfill() and then do a moving 2-element averaging window after that?
sr9497
sr9497 am 28 Mär. 2022
Bearbeitet: sr9497 am 28 Mär. 2022
I have multiple arrays with unequal lengths and put them into a matrix adding NaN to make it for example 4x4:
a = [20 10 5 10; 10 4 6 2; 5 8 10 1; NaN 8 NaN 4]
My actual matrix is a lot bigger, containing multiple NaN, but NaN is always in the fourth position of a column.
Getting the desired result is not a problem for the columns with 4 numbers but I am not able to get the same result in the columns that contain NaN.
Image Analyst
Image Analyst am 28 Mär. 2022
So now I'm getting confused. Do you have a row vector, or a 2-D matrix? You've shown both.
sr9497
sr9497 am 28 Mär. 2022
A 2-D matrix, the row vector was a mistake.

Melden Sie sich an, um zu kommentieren.

Kategorien

Gefragt:

am 27 Mär. 2022

Kommentiert:

am 28 Mär. 2022

Community Treasure Hunt

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

Start Hunting!

Translated by