Infinite while loop for iterating data

2 views (last 30 days)
Vasiliki Charisi
Vasiliki Charisi on 13 Oct 2015
Edited: Thorsten on 13 Oct 2015
I have a data-set that has the following form
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
where the MMSI is the ID of different ships.I want to compute the distance between two different points and get as a result the MMSIs involved as well as the times. I've written the following code, which does not work as I described.
for i = 1:length(MM(:,5))
t = MM(i,5);
ind1 = i;
length(ind1);
lat1 = lat(ind1);
lon1 = lon(ind1);
while (t <= (MM(i,5)+5))
for j = 2:length(MM(j,1))
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
This code essentially does the following:
for i = 1:length(MM(:,5))
ind1 = i;
lat1 = lat(ind1);
lon1 = lon(ind1);
ind2 = MM(2,1);
lat2 = lat(ind2);
lon2 = lon(ind2);
w = MM(d,5);
t = MM(i,5)+5+1
dis = distance(lat1, lon1, lat2, lon2);
if dis<=1
contact = [ind1, ind2, t, w];
end
end
and the while loop becomes an infinite loop. How can I write a while loop, in which I will take the elements from the second row and below and compute the distance from the element in the first row as long as the condition in the while is true? And after the distance is computed I want to get the contact matrix which contains the two different MMSIs and the times

Answers (2)

Eng. Fredius Magige
Eng. Fredius Magige on 13 Oct 2015
Hi you data MM not involved and should be called for execution MM=[] % being known as first matrix
I think for i = 1:length(MM(:,5)) be for i = 1:length(MM(:,6))
  1 Comment
Vasiliki Charisi
Vasiliki Charisi on 13 Oct 2015
Yes, it should be for
i=1:length(MM(:,6))
I didn't noticed it when I wrote the question. But I do not understand what do you mean by:data MM not involved and should be called for execution MM=[] % being known as first matrix. Could you explain me please what you mean?

Sign in to comment.


Thorsten
Thorsten on 13 Oct 2015
Edited: Thorsten on 13 Oct 2015
You can compute the distances between all pairs as
allpairs = nchoosek(1:8,2);
for i=1:numel(allpairs)
i1 = allpairs(i,1); i2 = allpairs(i,2);
d(i) = distance(lat(i1), lon(i1), lat(i2), lon(i2));
end
And select identifiers and time
ind = d > 1;
i1 = allpairs(ind,1); i2 = allpairs(ind,2);
IDandTime = [mmsi(i1) mmsi(i2) time(i1) time(i2) ];

Community Treasure Hunt

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

Start Hunting!