Straight line approximation of elements in array

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 日

0 投票

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 日
Didn't work. Doesn't say why
Matt J
Matt J 2022 年 3 月 22 日
編集済み: Matt J 2022 年 3 月 22 日
You received no output or error messages? In any case, I have now run your example in my post so that you can see that it works if your Matlab environment is normal.
Steven Lord
Steven Lord 2022 年 3 月 22 日
What does "didn't work" mean?
  • Do you receive warning and/or error messages? If so the full and exact text of those messages (all the text displayed in orange and/or red in the Command Window) may be useful in determining what's going on and how to avoid the warning and/or error.
  • Does it do something different than what you expected? If so, what did it do and what did you expect it to do?
  • Did MATLAB crash? If so please send the crash log file (with a description of what you were running or doing in MATLAB when the crash occured) to Technical Support using the Contact Support link on the Support section of the MathWorks website so we can investigate.
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 件)

カテゴリ

ヘルプ センター および File ExchangeInterpolation of 2-D Selections in 3-D Grids についてさらに検索

質問済み:

2022 年 3 月 22 日

編集済み:

2022 年 3 月 22 日

Community Treasure Hunt

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

Start Hunting!

Translated by