New point calculation - for loop

Hi there!
Ive got a question regarding new point calculation... So i got 2 Points (Latitude,Longitude) and I want to calculate 7198 new points between those two... So because there are so many i wanted to use a for loop.
  1. so first step is to calculate the direction angle (this one i did get correct)
  2. then calculate the distance between the points with pythagoras : 2700
  3. new array s = zeros(7200,1) where i want to put the previously (2.) calculated distances ++
  4. for loop:
for j = 1:7200
s(j,1) = distance ++; <- Here is the problem
end

回答(1 个)

No need for a loop:
% arbitrary point for beginning (perhaps longitude)
x1 = randi([1 1000],1);
y1 = randi([1 1000],1);
% arbitrary point for ending (perhaps latitude)
x2 = randi([1 1000],1);
y2 = randi([1 1000],1);
n = 7200; % number of points from beginning to ending
pf = polyfit([x1 x2], [y1 y2], 1);
x = linspace(x1, x2, n);
y = polyval(pf,x);

5 个评论

thats amazing!! thanks
Is there a way to include multiple points to interpolate beween them?
You're welcome.
I'm not sure what you mean by your follow-up question. The vectors x and y in my answer include muliple points -- 7200, to be precise. They inherently interpolate along a straight line between the two end points.
Yes my question is if there is a way to include multipe beginnings and endings with for example a for loop? So I dont have to write this code over and over again or 50 coordinates
Hmm, yes, I see. There are ways to shorten this, but I think the code below achiveves what you are looking for. I'm adding the variable m and the term "trip" for each of your 50 beginning-ending points.
m = 50; % number of trips
% example x-y values for beginnings of m trips
x1 = randi([1 1000],1,m);
y1 = randi([1 1000],1,m);
% example x-y values for corresponding trip endings
x2 = randi([1 1000],1,m);
y2 = randi([1 1000],1,m);
n = 7200; % number of points for each trip
tripPath = zeros(m,n,2); % x-y points for m trips, n points each
for i=1:m
pf = polyfit([x1(i) x2(i)], [y1(i) y2(i)], 1);
x = linspace(x1(i), x2(i), n); % x points for ith trip
y = polyval(pf,x); % y points for ith trip
tripPath(m,:,1) = x;
tripPath(m,:,2) = y;
end
The matrix tripPath is a mxnx2 matrix. The first dimension is for the trip (1 to m), the second dimension organizes the points for the trip (1 to n), with the 3rd dimension holding the actual x (1) and y (2) points for the trips.

请先登录,再进行评论。

类别

提问:

2021-6-14

评论:

2021-6-14

Community Treasure Hunt

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

Start Hunting!

Translated by