我有一个包含不同类型数据的数据集,如下所示
MMSI Latitude Longitude Date Time Time (decimal)
277333444 59.8564 30.497 04-08-12 09:49 589
241025000 37.3462 24.8713 04-08-12 09:49 589
636012774 35.7931 28.7227 04-08-12 11:29 680
248681000 36.9327 27.3042 04-08-12 11:52 703
312847000 35.3429 28.5582 04-08-12 11:52 703
239737000 36.4827 27.9405 04-08-12 11:52 703
200 36.7672 34.6444 04-08-12 12:52 763
237071900 36.7778 24.578 04-08-12 12:52 763
,数据从CSV文件导入。在样本数据中时间是排序的,但在数据中时间是混合的。对于数据,我想计算两个不同点之间的距离,并得到所涉及的mmsi以及时间。我编写了下面的代码,它几乎完成了我所描述的工作。
fid = fopen('vessel-movements-2.csv');
C = textscan(fid, '%f %f %f %f %f %s %s %s %f', 'HeaderLines', 1, 'Delimiter', ',');
fclose(fid);
iscell(C);
M = cell2mat(C(:,1:5));
DateString = (C{1,7});
formatIn = 'dd-mm-yy';
P = datenum(DateString, formatIn);
M = horzcat(M,P);
Q = cell2mat(C(:,9));
M = horzcat(M,Q);
a = M(:,6);
b=unique( a );
c = M(:,1);
d = unique(c);
lat = M(:,2);
lon = M(:,3);
time = M(:,7);
TimeStamp = P+time;
M = horzcat(M,TimeStamp);
MM = sortrows(M,8);
all(M(:,8) == MM(:,8))
for i = 1:length(MM(:,8))
t = MM(i,8);
ind1 = i;
length(ind1);
lat1 = lat(ind1);
lon1 = lon(ind1);
while (t <= (MM(i,8)+5))
for j = 2:length(d)
ind2 = j;
length(ind2);
lat2 = lat(ind2);
lon2 = lon(ind2);
w = MM(j,8);
end
dis = distance(lat1, lon1, lat2, lon2);
t = t+1;
end
if dis<=1
contact = [ind1, ind2, t, w];
end
end
脚本中的索引与显示的数据中的索引略有不同,但是我的数据中还有其他列,这些列在循环中没有使用,因此为了更好地可视化,我将它们从示例中删除。它第一次正确地计算了距离,但在外部循环中,程序似乎滞后并永远运行,但没有任何结果。我试过调试器,但结果是一样的。
这不是一个答案,而是一个格式化的注释。我只是想指出你的循环非常奇怪和不正常。在当前版本中,实际上是这样做的:
for i = 1:length(MM(:,8)) %this should be size(MM,1)
ind1 = i;
lat1 = lat(ind1);
lon1 = lon(ind1);
ind2 = d;
lat2 = lat(ind2);
lon2 = lon(ind2);
w = MM(d,8);
t = MM(i,8)+5+1
dis = distance(lat1, lon1, lat2, lon2);
if dis<=1
contact = [ind1, ind2, t, w];
end
end