Lookup value in one array based on interpolated point in another?

Sorry, this is probably pretty basic. I could do it with a function manually, but there must be an easier way. It's basically an interpolated lookup table.
I have two arrays of data, one is time with a fixed sample rate. Ex: 0, 0.1, 0.2, 0.3, etc
The other array is distance measured at the same sample rate. Both arrays are the same length because the distance was calculated as a function of time.
I simply want to be able to enter certain distances and have it tell me the time at that point. Of course the distancs (and times) will rarely fall on the specific interval so interpolation is required. It looks like "tablelookup()" might do this, but i don't have simscape. I only have basic Matlab and Simulink.
What's the best way to approach this? Thanks a lot.

 채택된 답변

Fangjun Jiang
Fangjun Jiang 2020년 10월 16일

0 개 추천

interp1() in MATLAB
"1-D Lookup Table" block in Simulink.

댓글 수: 1

Dan D
Dan D 2020년 10월 16일
편집: Dan D 2020년 10월 16일
Thanks i was reading about that but couldn't quite get it to work. Knowing that it shoudl work, i'll keep trying. Thanks
Update: i got it with interp1. Thanks!

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

추가 답변 (1개)

Ameer Hamza
Ameer Hamza 2020년 10월 16일
interp1() in itself is not suitable for developing an inverse mapping. Doing so will result in a mapping that does not work in both directions. A more suitable approach is to use interp1() with fsolve(). However, the distance must have a one-to-one relation with time, i.e., if you have the same distance value for two different time values, then it will cause issues. Check this example,
t = 0:0.1:1;
dist = t.^2; % a one-to-one function
dist_fun = @(tq) interp1(t, dist, tq);
distq = 0.5; % distance value for which you want the time value
tq_sol = fzero(@(tq) dist_fun(tq)-distq, rand());
To verify
>> dist_fun(tq_sol)
ans =
0.5000 % got same value as distq

댓글 수: 1

Can you explain what you mean by not working in both directions?
Thanks

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

카테고리

도움말 센터File Exchange에서 Interpolation에 대해 자세히 알아보기

제품

릴리스

R2020b

질문:

2020년 10월 16일

댓글:

2020년 10월 16일

Community Treasure Hunt

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

Start Hunting!

Translated by