How Does interp1 Work if the Sample Points are not Monotonic?
28 ビュー (過去 30 日間)
古いコメントを表示
Forever I've thought that the sample points input to @doc:interp1 have to be distinct AND monotonic. But I just saw on the doc page that distinct is the only requirement.
For example:
matlabRelease
x = [1,3,0,2]; % not monotonic
y = sin(x);
xq = 1.5;
yi = interp1(x,y,xq)
I'm quite surprised by this result.
Does interp1 sort the sample points and reorder the sample values if the sample points are not monotonic?
The same answer is obtained:
isequal(yi,interp1(sort(x),sin(sort(x)),xq))
0 件のコメント
採用された回答
Stephen23
2025 年 12 月 27 日 19:48
INTERP1 automatically sorts the sample points internally if they're not monotonic, and it reorders the corresponding sample values accordingly:
xs = [3, 1, 4, 2];
ys = xs.^2; % [9, 1, 16, 4]
xq = 2.5;
yi = interp1(xs, ys, xq)
[xz, idx] = sort(xs);
yz = ys(idx);
yj = interp1(xz, yz, xq)
3 件のコメント
Stephen23
2025 年 12 月 27 日 20:42
"Do you recall if that's always been the behavior?"
I don't recall ever using this, nor does the documentation in the wayback machine seem to mention it.
"Any opinion on if that should be the behavior, or if such an input should throw an error?"
Does this edge-case handling takes significant processing time for all users? That would be unfortunate... but I have no idea what all the use-cases are that TMW have specified for this function (the fact that it is not documented indicates that it might not be very significant).
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Startup and Shutdown についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!