I have some simple code for linearly interpolating inbetween two points, the code will be below,
function interp = lerp(A,B,t)
interp = t*B + A*(1 - t);
end
This functions works perfectly when I put in a single value for t, however it breaks when I try to put in a vector. My desired function will essentially give me a vector back which will be the linearly interpolated points for each value of t, essentially running the function for each value of t.

3 件のコメント

KSSV
KSSV 2021 年 11 月 6 日
Show us the full code which you tried along with input.
Sudharsana Iyengar
Sudharsana Iyengar 2021 年 11 月 6 日
Since your question is not complete.
I am assuming A and B are not of compatible shape. Or both A and B same size?
How about 't' is it scalar or is it also a vector. if it is vector then you might have to check the size of A, B and t. Then you might need to use .* (dot and a star) instead of just *.
Yongjian Feng
Yongjian Feng 2021 年 11 月 6 日
It seems like A and B are scalars and t is a vector, according to the description. But if that is the case, it should work.

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

 採用された回答

Matt J
Matt J 2021 年 11 月 6 日

0 投票

function interp = lerp(A,B,t)
t=t(:).';
interp = B(:)*t + A(:)*(1 - t);
end

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File Exchange で Interpolation についてさらに検索

製品

質問済み:

2021 年 11 月 6 日

回答済み:

2021 年 11 月 6 日

Community Treasure Hunt

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

Start Hunting!

Translated by