Lookup value in one array based on interpolated point in another?
8 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Dan D
el 16 de Oct. de 2020
Comentada: Dan D
el 16 de Oct. de 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.
0 comentarios
Respuesta aceptada
Fangjun Jiang
el 16 de Oct. de 2020
interp1() in MATLAB
"1-D Lookup Table" block in Simulink.
1 comentario
Más respuestas (1)
Ameer Hamza
el 16 de Oct. de 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
Ver también
Categorías
Más información sobre Signal Operations en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!