Coordinate Interpolation between mutiple points

2 visualizzazioni (ultimi 30 giorni)
em_++
em_++ il 21 Giu 2021
Commentato: Bjorn Gustavsson il 21 Giu 2021
Hi there!
Ive got a question. So i have 13 coordinates, taken every second hour.
Now as i want to do some plotting i need Coordinates for every second ... So my idea is to interpolate between the points.
My code for now :
m = 13; % coordinates
n = 7198 %Number of seconds between 2 hours
tripPath = zeros(m,n,2);
for 1 = 1:m
pf = polyfit([Lat(:,1) Lat(:,1)], [Long(:,2) Long(:,2)]) %Coordinates are in an Array
x = linspace(Lat(:,1), Lat(:,1),n);
y = polyval(pf, x);
tripPath(m,:,1) = x;
tripPath(m,:,2) = y
end
This code works for 2 Points already if i leave out m and the for loop... I dont realy know how to implement this for multiple coordinates.
Thanks for your help already!!
  1 Commento
em_++
em_++ il 21 Giu 2021
And I was also thinking maybe it does not work because matlab overwrites my new coordinates every loop? So maybe the answer is to write the coordinates into a seperate array so they dont get overwritten?

Accedi per commentare.

Risposte (2)

Walter Roberson
Walter Roberson il 21 Giu 2021
m = 13; % coordinates
n = 7198 %Number of seconds between 2 hours
tripPath = zeros(m,n,2);
for 1 = 1:m
The syntax for for loops requires a variable name to the left of the = not a number.
pf = polyfit([Lat(:,1) Lat(:,1)], [Long(:,2) Long(:,2)]) %Coordinates are in an Array
You do not write to Lat or Long inside the loop, and you are not using any loop control variable in that statement, so every iteration you would be calculating the same thing. When you always calculate the same thing, it is more efficient to move it to before the loop.
x = linspace(Lat(:,1), Lat(:,1),n);
Again you do not change Lat or n inside the loop so you always calculate the same thing, so you might as well do that before the loop.
y = polyval(pf, x);
Because pf is always the same for every iteration and because x is always the same for every iteration, then you would be calculating the same y every iteration, so you might as well do that before the loop.
tripPath(m,:,1) = x;
tripPath(m,:,2) = y
x and y are always the same for every iteration. You do use the variable m as an index, but you never change m, so you are always writing to the same place; if that was deliberate then you might as well do it before the loop.
end
End of the loop, and you have no statements left inside of the loop, so you might as well not do the loop.
  2 Commenti
em_++
em_++ il 21 Giu 2021
oh sorry!
maybe like this? becaus in total i got 50 coordinates and it would be quiet a bit of code if i wrote this for every coordinate
m = 13; % coordinates
n = 7198 %Number of seconds between 2 hours
tripPath = zeros(m,n,2);
for 1 = 1:m
pf = polyfit([Lat(i,1) Lat(i+1,1)], [Long(i,2) Long(i+1,2)]) %Coordinates are in an Array
x = linspace(Lat(i,1), Lat(i+1,1),n);
y = polyval(pf, x);
tripPath(m,:,1) = x;
tripPath(m,:,2) = y
end
Bjorn Gustavsson
Bjorn Gustavsson il 21 Giu 2021
Try the suggestion with interp1.

Accedi per commentare.


Bjorn Gustavsson
Bjorn Gustavsson il 21 Giu 2021
Modificato: Bjorn Gustavsson il 21 Giu 2021
To me it sounds like you in principle want to interpolate both your coordinates with respect to time between your hourly samples. That can be most easily done in this simplistic manner:
t_i = 0:(2*3600);
Lat_i = interp1(t_hourly,lat_hourly,t_i,'pchip'); % Arbitrary choise of interpolation method
Long_i = interp1(t_hourly,long_hourly,t_i,'pchip');
% you might have to make sure that the dimensions match in the
% interpolation-call, my version seems to require that the first dimension
% of long and lat_hourly match up with the time-array.
There's also this file-exchange submission that might be of interest:
HTH

Categorie

Scopri di più su Data Type Conversion in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by