New point calculation - for loop

1 view (last 30 days)
em_++
em_++ on 14 Jun 2021
Commented: em_++ on 14 Jun 2021
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

Answers (1)

Scott MacKenzie
Scott MacKenzie on 14 Jun 2021
Edited: Scott MacKenzie on 14 Jun 2021
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 Comments
Scott MacKenzie
Scott MacKenzie on 14 Jun 2021
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.

Sign in to comment.

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!