How to find interpolated values when there are multiple?

조회 수: 29 (최근 30일)
Adam Hockley
Adam Hockley 2018년 5월 9일
댓글: Sahil Wani 2022년 8월 15일
I'm looking to find points where a vector crosses a certain threshold. I have been using interp1 to try to accomplish this but it does not recognise multiple solutions.
for example:
a = [1 2 3 5 8 4];
b = interp1(a, 1:length(a), 6);
returns 4.3333
This is one of the correct answers, but I am looking to get this to return both points where the interpolated value would be 6 (4.33 and 5.5). Any help much appreciated!

채택된 답변

John D'Errico
John D'Errico 2018년 5월 9일
편집: John D'Errico 2018년 5월 9일
interp1 assumes a single valued functional relationship. You cannot use interp1 to do what you wish to do.
Instead, use a tool like Doug Schwarz's intersections , downloaded from the file exchange.
a = [1 2 3 5 8 4];
intersections(1:length(a),a,[1 6],[6 6])
ans =
4.3333
5.5
  댓글 수: 4
Walter Roberson
Walter Roberson 2022년 8월 15일
Is that your independent variable? If so then it duplicates the range 4:.5:8; what would you like to have happen in that range?
If you want to get both outputs, then I recommend you break the problem up into two interp1 searches,
a1 = 0:0.5:8; a2 = 8:-0.5:4;
b1 = values for 0 to 8; b2 = values for 8 to 4
q = list of points to query
out1 = interp1(a1, b1, q(:));
out2 = interp1(a2, b2, q(:));
out = [out1, out2];
This would have two columns, one for results for 0 to 8, and the other for results for the 8 to 4. q values not present in a range would give nan in the output, which you could detect with isnan();
Sahil Wani
Sahil Wani 2022년 8월 15일
Thanks @Walter Roberson! Very useful.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Multirate Signal Processing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by