Computing distance between vectors and finding the ids of the points and the time
1 view (last 30 days)
Show older comments
I have a data set that has the following form:
mmsi lat lon time
500 30.83 47.56 140
502 45.78 49.06 140
... ... ... ...
500 31.54 47.67 141
502 44.56 48.89 141
where mmsi is the id of a ship, lat/lon are the geographic coordinates and time is the time where the data is gathered. In the dataset there are many different ships and the samples are taken in many different times. I want to iterate through the different ships and call a function. This function, I want to compute the distance between two different ships and check if it is smaller than a certain distance. If this is true I want the function return the corresponding ids of the ships as well as the starting and finishing time, for which the condition is true. This is how to call the function and how I get the unique ids
c = M(:,1);
d = unique(c);
%# distanceFinder
for j=1:length(d)
in=find(c==d(j));
length(in);
[id1,id2,time1,time2] = distanceFinder(M(:,1),M(:,2),M(:,3),M(:,7));
end
and this is the function I wrote.
function [id1, id2, time1, time2] = distanceFinder(mmsi,lat,lon,time)
R = 6371;
x = R*cos(lat).*cos(lon);
y = R*cos(lat).*sin(lon);
m = [x,y];
for i=1:x(end)
plot3(x,y,time);
d = dist(m, 'euclidean');
t = time2 - time1;
while (d<=500 && t<=5)
id1 = mmsi(m(i));
id2 = mms1(m(i+1));
time1 = time(d);
time2 = time(d+1);
[id1, id2, time1, time2]
end
end
end
I am at a loss how to make this function work, following the logic I described. Any help would be appreciated.
1 Comment
Chad Greene
on 5 Oct 2015
A few things:
1. You probably don't want to be plotting within the loop unless you're doing some sort of animation.
2. For loops, avoid using the variable names i and j because Matlab thinks they're the imaginary unit. It usually doesn't cause problems, but when it does, it can be a headache to track down.
3. This bit: for i=1:x(end) looks wrong. Do you mean for i=1:length(x), or in keeping with the point above, for k=1:length(x)?
Accepted Answer
Chad Greene
on 5 Oct 2015
Edited: Chad Greene
on 5 Oct 2015
Check out John D'Errico's inter-point distance matrix function on File Exchange.
mmsi = repmat((500:510)',5,1);
lat = 35+5*randn(size(mmsi));
lon = 45+3*randn(size(mmsi));
t = repmat(140,11,1);repmat(141,11,1);repmat(142,11,1);repmat(143,11,1);repmat(143,11,1)];
%
R = 6371;
x = R*cos(lat).*cos(lon);
y = R*cos(lat).*sin(lon);
%
% inter-point distance matrix:
s = ipdm([x y]);
%
% indices of all ships within 500 km of another ship:
ind = s>0 & s<500;
0 Comments
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!