how to find a point on the grid

  • Follow


Dear everybody,
I have this code. But it appears to not work correctly for small cell sizes.
The first step in my code is to define the gridded area of interest defined by boxx and boxy which I create bellow. I am then given two vectors 
lon_gsod_tm = [-123 -70]
lat_gsod_tm = [35 34]
I have to find to which box on my created grid these two (or more) points belong two. I put these indecies into box_index cell array. Sometimes (depending on the cell size) the box_index{i} will contain several points. Other times only one point or no points at all. Right now the program seems to have problem when the point fall on the edge of the box....I know I am not suppose to complain but...grrrrrrr....I guess if somebody has a solution I would be very thankful!!!!


% the area of interest
lon1 = -65;
lon2 = -125;
lat1 = 22;
lat2 = 49;

% box size
box_x = 2.0;
box_y = 2.0;

% creating the grid
kx = 1;
ky = 1;
boxx = [];
boxy = [];
for ix=lon2:box_x:lon1
 for iy=lat1:box_y:lat2 
   boxx(kx,ky) = ix;
   boxy(kx,ky) = iy;
   ky = ky + 1;
 end
 ky = 1;
 kx = kx + 1;
end


lon_gsod_tm = [-123 -70]
lat_gsod_tm = [35 34]
% box_index contains the indecies of data falling into the same "box"
box_index = {};
ka        = 1;
% flag is for initialization
flag      = 1;
si        = size(boxx);
for i = 1:si(1)-1
 for j = 1:si(2)-1
  for data = 1:length(lon_gsod_tm)
   % flag_eq is to for the special case when we are on the edge of the
   % box; we will set it to one when it is the case
   flag_eq = 0;
   if  lon_gsod_tm(data) <= boxx(i+1,j) & lon_gsod_tm(data) >= boxx(i,j)
    if lat_gsod_tm(data) <= boxy(i,j+1) & lat_gsod_tm(data) >= boxy(i,j)
     if  lon_gsod_tm(data) == boxx(i+1,j) | lon_gsod_tm(data) == boxx(i,j)
      if lat_gsod_tm(data) == boxy(i,j+1) | lat_gsod_tm(data) == boxy(i,j)
       flag_eq = 1;
       str = 'hello'
       if flag == 1
        box_index{ka} = data;
        flag = 0;
       else
        ind = find(box_index{ka-1}==data);
        if length(ind)==0
         box_index{ka-1} = cat(1, box_index{ka-1}, data);
        end
       end
      end
     end
     % if we have added the index because the datum is on the boarder, do
     % not do anythinn
     if flag_eq == 1
      continue 
     end
     if flag == 1
      box_index{ka} = data;
      flag = 0;
     else
      box_index{ka} = cat(1, box_index{ka}, data);
     end
    end
   end
  end
  ka   = ka + 1;
  flag = 1;
 end
end
0
Reply jenya 3/5/2010 9:02:20 PM


0 Replies
268 Views

(page loaded in 0.046 seconds)

Similiar Articles:













7/15/2012 7:30:34 AM


Reply: