You are adding 1 to i, because Matlab has no way of knowing that you mean the next value.
In the find command, you should really add some parentheses to make sure that you are actually getting what you mean.
You are overwriting stns each loop. What I would do if I were you is making stns a cell.
n=1000;
coordinates=[randperm(n)', rand(n,1)*0.4+103.6, rand(n,1)*0.25+1.25];
lat = coordinates(:,2);
lon = coordinates(:,3);
i_vector=103.6:(1/75):104;
j_vector=1.25:(1/120):1.5;
stns=cell(length(i_vector),length(j_vector));
for i = 1:(length(i_vector)-1)
for j = 1:(length(j_vector)-1)
index = (lat > i_vector(i)) ...
& (lat < i_vector(i+1)) ...
& (lon > j_vector(j)) ...
& (lon < j_vector(j+1)) ;
stns{i,j} = coordinates(index,1);
end
end
Now you can look in the variable editor to see what stations are in what grid location. If you want to have the row number you can add this line coordinates(:,4)=1:size(coordinates,1); and change the ,1 to ,4 in the line that writes to stns.
PS I think this could be done more elegantly (and faster for bigger datasets), but this is most similar to your code, easy to understand, and not absurdly slow for most lists of stations.
Best Answer