Calculate linear interpolation of a vector

81 ビュー (過去 30 日間)
Nicola Caldognetto
Nicola Caldognetto 2017 年 11 月 6 日
回答済み: Star Strider 2017 年 11 月 6 日
Hi, I have this problem that I can't resolve. I have a column vector with a numerical value at the beginning of it and at the end example
X=[4 0 0 0 0 0 0 8]'
I need to calculate all the intermediate value making the average of the two values. Example in the position on the center o the vector it should be (8+4)/2=6
I have tried with a recursive method but I can't resolve it. Is there a simply way to do that?

回答 (2 件)

Star Strider
Star Strider 2017 年 11 月 6 日
If you actually want to interpolate, this works:
X = [4 0 0 0 0 0 0 8]';
Xnz = X(X ~= 0); % Non-Zero Elements
vnz = find(X ~= 0); % Indices Of Non-Zero Elements
iv = 1:length(X); % Interpolation Vector
Xi = interp1(vnz, Xnz, iv, 'linear')' % Interpolate To Get Desired Output
Xi =
4.00
4.57
5.14
5.71
6.29
6.86
7.43
8.00
Extrapolation isn’t necessary here, but could be if you had zeros at the ends of the vector.

Matt J
Matt J 2017 年 11 月 6 日
編集済み: Matt J 2017 年 11 月 6 日
xnew=linspace(x(1), x(end), numel(x))
  2 件のコメント
Nicola Caldognetto
Nicola Caldognetto 2017 年 11 月 6 日
Thanks! What's numel(x)?
Matt J
Matt J 2017 年 11 月 6 日
The (num)ber of (el)ements in x.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeInterpolation についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by