MATLAB: How can i fix this? im new and need your help. it just calculate firs two rows of days between Days>=0 && Days<=7.

matlab functionwhile loop

Hi, i have problem with my code, i have 85X10 double, that first and second rows are latitude and longitude, other 85X1 double is the days between each events. i need to calculate distance of each event and remove event with distance(Km) >=0 & <=5 and days between Days>=0 && Days<=7. i wrote something but still left events with that condition. i need your help
radius = 6371;
Fd = [];
s = 1;
for i = 1: length(CatTd)-1
for ii = 2:length(CatTd)-1
if CatTd(i,3:4) == CatTd(ii,3:4) % Row 3(Year) Row 4(Month)
lat1 = CatTd(i,2)*pi/180; lat2 = CatTd(ii,2)*pi/180;
lon1 = CatTd(i,1)*pi/180; lon2 = CatTd(ii,1)*pi/180;
deltaLat1 = lat2-lat1; deltaLon1 = lon2-lon1;
x = deltaLon1*cos((lat1+lat2)/2); y = deltaLat1;
Dkm = radius*sqrt(x*x + y*y); % Distance(Km)

else
i = i+1; ii = i+1;
lat1 = CatTd(i,2)*pi/180; lat2 = CatTd(ii,2)*pi/180;
lon1 = CatTd(i,1)*pi/180; lon2 = CatTd(ii,1)*pi/180;
deltaLat1 = lat2-lat1; deltaLon1 = lon2-lon1;
x = deltaLon1*cos((lat1+lat2)/2); y = deltaLat1;
Dkm = radius*sqrt(x*x + y*y); % Distance(Km)
end
if Dkm>=0 && Dkm<=5
Fd(s) = i; Fd(s+1) = ii; s = s+2;
end
while ii == length(CatTd)
i = i+1;
ii = i+1;
end
while i == length(CatTd)
ii = ii+1;
end
end
end
end
end

Best Answer

to calculate distances you can use distance if you have Mapping or Antenna toolboxes, otherwise this one looks verry promising.
The rest can be achieved using logical indexing as Rik pointed out, if I got you right, something like that should get it done:
% random distances and times between events
dist = rand(85,1)*15;
time = rand(85,1)*21;
% generate logical index for the events with distance between 0 and 5 km OR time between 0 and 7 days
flags = (dist >= 0 & dist <= 5) | (time >= 0 & time <= 7);
% throw away those events
dist(flags) = [];
time(flags) = [];