MATLAB Answers

Coordinate Interpolation between mutiple points

2 views (last 30 days)
em_++
em_++ on 21 Jun 2021
Commented: Bjorn Gustavsson on 21 Jun 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 Comment
em_++
em_++ on 21 Jun 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?

Sign in to comment.

Answers (2)

Walter Roberson
Walter Roberson on 21 Jun 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 Comments

Sign in to comment.


Bjorn Gustavsson
Bjorn Gustavsson on 21 Jun 2021
Edited: Bjorn Gustavsson on 21 Jun 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

Community Treasure Hunt

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

Start Hunting!