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

1 回表示 (過去 30 日間)
Dan D
Dan D 2020 年 10 月 16 日
コメント済み: Dan D 2020 年 10 月 16 日
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
  1 件のコメント
Dan D
Dan D 2020 年 10 月 16 日
Can you explain what you mean by not working in both directions?
Thanks

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeSignal Operations についてさらに検索

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by