MATLAB Answers

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

12 views (last 30 days)
Dan D
Dan D on 16 Oct 2020
Commented: Dan D on 16 Oct 2020
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.

Accepted Answer

Fangjun Jiang
Fangjun Jiang on 16 Oct 2020
interp1() in MATLAB
"1-D Lookup Table" block in Simulink.
  1 Comment
Dan D
Dan D on 16 Oct 2020
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!

Sign in to comment.

More Answers (1)

Ameer Hamza
Ameer Hamza on 16 Oct 2020
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 Comment
Dan D
Dan D on 16 Oct 2020
Can you explain what you mean by not working in both directions?

Sign in to comment.




Community Treasure Hunt

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

Start Hunting!