Straight line approximation of elements in array

7 次查看(过去 30 天)
I have a 21x1 array in which every other value is NaN:
s=[0 NaN 5 NaN 0 NaN 0 NaN 5 NaN 0 NaN 0 NaN 5 NaN 0 NaN 0 NaN 5];
I need to make a straight line from one surrounding value to the other for each NaN. For this case, it would be the averages, like 0 NaN 5 would be 2.5, because the surrounding values are 0 and 5, which would make the NaN become 2.5.
Basically I need to replace all the NaNs in this fashion and plot the new array.

采纳的回答

Matt J
Matt J 2022-3-22
编辑:Matt J 2022-3-22
s=[0 NaN 5 NaN 0 NaN 0 NaN 5 NaN 0 NaN 0 NaN 5 NaN 0 NaN 0 NaN 5];
fillmissing(s,'linear')
ans = 1×21
0 2.5000 5.0000 2.5000 0 0 0 2.5000 5.0000 2.5000 0 0 0 2.5000 5.0000 2.5000 0 0 0 2.5000 5.0000
  5 个评论
Pesach Nestlebaum
Pesach Nestlebaum 2022-3-22
Execution of script fillmissing as a function is not supported:
C:\Users\hipes\Downloads\fillmissing.m
Error in sf (line 2)
fillmissing(s,'linear')
Matt J
Matt J 2022-3-22
编辑:Matt J 2022-3-22
Did you download an mfile called fillmissing.m to the folder
C:\Users\hipes\Downloads\
If you aren't using it for something else, I suggest you get rid of it.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by