Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
divide area into boxes

Subject: divide area into boxes

From: jenya polyakova

Date: 18 May, 2010 22:03:04

Message: 1 of 4

Dear everybody,
for my data I have 30713 stations (i.e. the size of my vector is 1x30713). When I do code below , I am left with 25703 stations, which is wrong: I still have to have 30713. Essentially, the code below divides region study area into equal size boxes of size 10x10. It then stores the indecies of original data into cell structure where each cell is box of size 10x10.
I am not sure where I went wrong. It is, I think, the border effect. If anybody could help me with it, it would great. It is very easy code....

% Define region of study
lon_e = -125.0
lon_w = -70.0
lat_s = 25.0
lat_n = 50.0
%-----------------------------------------------------------------------
% Define box size
box_x = 10
box_y = 10
%-----------------------------------------------------------------------
% create boxes.
cnt = 1;
for ix = lon_e:box_x:lon_w
     x(cnt) = ix;
     cnt = cnt+1;
end
cnt = 1;
for iy = lat_s:box_y:lat_n
     y(cnt) = iy;
     cnt = cnt+1;
end
%---------------------------------------------------------------------
% now we will find indecies of station which belong to the particular box
cnt = 1;
for i = 1 : (length(x)-1)
  for j = 1 : (length(y)-1)
    % note lon and lat are vectors of coordinates of our original data.
    jdx = find(x(i) < = lon & lon<=x(i+1) & y(j) <= lat & lat<= y(j+1));
    % store these indecies into structure
    if length(jdx) > 0
      index{cnt} = jdx;
      cnt = cnt + 1;
    end
  end
end

Subject: divide area into boxes

From: Walter Roberson

Date: 18 May, 2010 22:23:12

Message: 2 of 4

jenya polyakova wrote:
> Dear everybody,
> for my data I have 30713 stations (i.e. the size of my vector is
> 1x30713). When I do code below , I am left with 25703 stations, which is
> wrong: I still have to have 30713. Essentially, the code below divides
> region study area into equal size boxes of size 10x10. It then stores
> the indecies of original data into cell structure where each cell is box
> of size 10x10.
> I am not sure where I went wrong. It is, I think, the border effect. If
> anybody could help me with it, it would great. It is very easy code....
>
> % Define region of study
> lon_e = -125.0
> lon_w = -70.0
> lat_s = 25.0
> lat_n = 50.0
> %-----------------------------------------------------------------------
> % Define box size
> box_x = 10
> box_y = 10
> %-----------------------------------------------------------------------
> % create boxes.
 > cnt = 1;
> for ix = lon_e:box_x:lon_w
> x(cnt) = ix;
> cnt = cnt+1;
> end
> cnt = 1;
> for iy = lat_s:box_y:lat_n
> y(cnt) = iy;
> cnt = cnt+1;
> end

Why not use

x = lon_e:box_e:lon_w;
y = lat_s:box_y:lat_n;

> %---------------------------------------------------------------------
> % now we will find indecies of station which belong to the particular box
> cnt = 1;
> for i = 1 : (length(x)-1)
> for j = 1 : (length(y)-1)
> % note lon and lat are vectors of coordinates of our original data.
> jdx = find(x(i) < = lon & lon<=x(i+1) & y(j) <= lat & lat<= y(j+1));

When you use equalities on both sides of the test, you count boundary values
in two boxes.


> % store these indecies into structure
> if length(jdx) > 0
> index{cnt} = jdx;
> cnt = cnt + 1;
> end

There: if a box is empty, you don't count it. If you are sure that none of the
boxes are empty, then you do not need to test the length.

By the way, isempty() is faster than checking the length > 0.

> end
> end

Subject: divide area into boxes

From: jenya polyakova

Date: 18 May, 2010 22:31:07

Message: 3 of 4

Walter Roberson <roberson@hushmail.com> wrote in message <hsv41i$o8p$1@canopus.cc.umanitoba.ca>...
> jenya polyakova wrote:
> > Dear everybody,
> > for my data I have 30713 stations (i.e. the size of my vector is
> > 1x30713). When I do code below , I am left with 25703 stations, which is
> > wrong: I still have to have 30713. Essentially, the code below divides
> > region study area into equal size boxes of size 10x10. It then stores
> > the indecies of original data into cell structure where each cell is box
> > of size 10x10.
> > I am not sure where I went wrong. It is, I think, the border effect. If
> > anybody could help me with it, it would great. It is very easy code....
> >
> > % Define region of study
> > lon_e = -125.0
> > lon_w = -70.0
> > lat_s = 25.0
> > lat_n = 50.0
> > %-----------------------------------------------------------------------
> > % Define box size
> > box_x = 10
> > box_y = 10
> > %-----------------------------------------------------------------------
> > % create boxes.
> > cnt = 1;
> > for ix = lon_e:box_x:lon_w
> > x(cnt) = ix;
> > cnt = cnt+1;
> > end
> > cnt = 1;
> > for iy = lat_s:box_y:lat_n
> > y(cnt) = iy;
> > cnt = cnt+1;
> > end
>
> Why not use
>
> x = lon_e:box_e:lon_w;
> y = lat_s:box_y:lat_n;
>
> > %---------------------------------------------------------------------
> > % now we will find indecies of station which belong to the particular box
> > cnt = 1;
> > for i = 1 : (length(x)-1)
> > for j = 1 : (length(y)-1)
> > % note lon and lat are vectors of coordinates of our original data.
> > jdx = find(x(i) < = lon & lon<=x(i+1) & y(j) <= lat & lat<= y(j+1));
>
> When you use equalities on both sides of the test, you count boundary values
> in two boxes.
>
>
> > % store these indecies into structure
> > if length(jdx) > 0
> > index{cnt} = jdx;
> > cnt = cnt + 1;
> > end
>
> There: if a box is empty, you don't count it. If you are sure that none of the
> boxes are empty, then you do not need to test the length.
>
> By the way, isempty() is faster than checking the length > 0.
>
> > end
> > end

Hi, I am not sure I understand if box is empty there are not indecies in it. Yet, somewhere in this code I go from 30713 stations to 25703. The way I calculate the number of stations after I run this code is:
sum = 0
for i=1:length(index)
   sum=sum+length(index{i})
end
sum = 25703. Any suggestions. Thank you for your other suggestions. I really appreciate it.

Subject: divide area into boxes

From: Roger Stafford

Date: 19 May, 2010 23:35:21

Message: 4 of 4

"jenya polyakova" <jenya56@yahoo.com> wrote in message <hsv2qo$1sj$1@fred.mathworks.com>...
> Dear everybody,
> for my data I have 30713 stations (i.e. the size of my vector is 1x30713). When I do code below , I am left with 25703 stations, which is wrong: I still have to have 30713. Essentially, the code below divides region study area into equal size boxes of size 10x10. It then stores the indecies of original data into cell structure where each cell is box of size 10x10.
> I am not sure where I went wrong. It is, I think, the border effect. If anybody could help me with it, it would great. It is very easy code....
>
> % Define region of study
> lon_e = -125.0
> lon_w = -70.0
> lat_s = 25.0
> lat_n = 50.0
> %-----------------------------------------------------------------------
> % Define box size
> box_x = 10
> box_y = 10
> %-----------------------------------------------------------------------
> % create boxes.
> cnt = 1;
> for ix = lon_e:box_x:lon_w
> x(cnt) = ix;
> cnt = cnt+1;
> end
> cnt = 1;
> for iy = lat_s:box_y:lat_n
> y(cnt) = iy;
> cnt = cnt+1;
> end
> %---------------------------------------------------------------------
> % now we will find indecies of station which belong to the particular box
> cnt = 1;
> for i = 1 : (length(x)-1)
> for j = 1 : (length(y)-1)
> % note lon and lat are vectors of coordinates of our original data.
> jdx = find(x(i) < = lon & lon<=x(i+1) & y(j) <= lat & lat<= y(j+1));
> % store these indecies into structure
> if length(jdx) > 0
> index{cnt} = jdx;
> cnt = cnt + 1;
> end
> end
> end

  When you write lon_e:box_x:lon_w, you get only

 x = [-125,-115,-105,-95,-85,-75]

What about any stations with longitude between -75 and =70? They get left out of 'index'. Similarly latitudes between 45 and 50 are left out. Would that account for the discrepancy? You should set all those limits so they are sure to include anything present in the original station data.

Roger Stafford

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us