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

1 次查看(过去 30 天)
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
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

类别

Help CenterFile Exchange 中查找有关 Signal Operations 的更多信息

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by