New point calculation - for loop
Mostrar comentarios más antiguos
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.
- so first step is to calculate the direction angle (this one i did get correct)
- then calculate the distance between the points with pythagoras : 2700
- new array s = zeros(7200,1) where i want to put the previously (2.) calculated distances ++
- for loop:
for j = 1:7200
s(j,1) = distance ++; <- Here is the problem
end
Respuestas (1)
Scott MacKenzie
el 14 de Jun. de 2021
Editada: Scott MacKenzie
el 14 de Jun. de 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 comentarios
em_++
el 14 de Jun. de 2021
Scott MacKenzie
el 14 de Jun. de 2021
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.
em_++
el 14 de Jun. de 2021
Scott MacKenzie
el 14 de Jun. de 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.
em_++
el 14 de Jun. de 2021
Categorías
Más información sobre Surface and Mesh Plots en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!