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:
grid comparison

Subject: grid comparison

From: David Craig

Date: 17 Nov, 2010 20:16:06

Message: 1 of 7

Hi all,

I've need to correlate two sets of data, each set is a grid with the centres of each cell associated with a value. However the grids are different sizes, so they have a different number of cells. I am trying to find the centres from one grid that appear inside each individual cell of the other and then average them.

My code......

for i=1:2508
    count=1;
    
    for m=1:816
        
        
        if (centlon(m) < lim(i,1)) ...
             && (centlon(m) > lim(i,2)) ...
                 && (centlat(m) < lim(i,3)) ...
                     && (centlat(m) > lim(i,4))
            
                         avg(count)=coup(m,4);
                         count=count+1;
            
        end
    

    end
    
    
    bev(i,5)=(sum(avg))/length(avg);
    
    
    
end

It runs but I get some very strange results in places, i.e. values where the grids do not overlap.

Any ideas????

Subject: grid comparison

From: Sean de

Date: 17 Nov, 2010 21:24:04

Message: 2 of 7

"David Craig" <dcdavemail@gmail.com> wrote in message <ic1d66$g2f$1@fred.mathworks.com>...
> Hi all,
>
> I've need to correlate two sets of data, each set is a grid with the centres of each cell associated with a value. However the grids are different sizes, so they have a different number of cells. I am trying to find the centres from one grid that appear inside each individual cell of the other and then average them.
>
> My code......
>
> for i=1:2508
> count=1;
>
> for m=1:816
>
>
> if (centlon(m) < lim(i,1)) ...
> && (centlon(m) > lim(i,2)) ...
> && (centlat(m) < lim(i,3)) ...
> && (centlat(m) > lim(i,4))
>
> avg(count)=coup(m,4);
> count=count+1;
>
> end
>
>
> end
>
>
> bev(i,5)=(sum(avg))/length(avg);
>
>
>
> end
>
> It runs but I get some very strange results in places, i.e. values where the grids do not overlap.
>
> Any ideas????

You reset count at the beginning of every i-loop. Is this really what you want? Are the results more what you expect when you move the count initialization outside the i-loop?

Subject: grid comparison

From: Sean de

Date: 17 Nov, 2010 21:44:04

Message: 3 of 7

"Sean de " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <ic1h5k$a7r$1@fred.mathworks.com>...
> "David Craig" <dcdavemail@gmail.com> wrote in message <ic1d66$g2f$1@fred.mathworks.com>...
> > Hi all,
> >
> > I've need to correlate two sets of data, each set is a grid with the centres of each cell associated with a value. However the grids are different sizes, so they have a different number of cells. I am trying to find the centres from one grid that appear inside each individual cell of the other and then average them.
> >
> > My code......
> >
> > for i=1:2508
> > count=1;
> >
> > for m=1:816
> >
> >
> > if (centlon(m) < lim(i,1)) ...
> > && (centlon(m) > lim(i,2)) ...
> > && (centlat(m) < lim(i,3)) ...
> > && (centlat(m) > lim(i,4))
> >
> > avg(count)=coup(m,4);
> > count=count+1;
> >
> > end
> >
> >
> > end
> >
> >
> > bev(i,5)=(sum(avg))/length(avg);
> >
> >
> >
> > end
> >
> > It runs but I get some very strange results in places, i.e. values where the grids do not overlap.
> >
> > Any ideas????
>
> You reset count at the beginning of every i-loop. Is this really what you want? Are the results more what you expect when you move the count initialization outside the i-loop?

I misinterpreted what the count was doing in the above. What do you mean by the grids don't overlap?

This will get rid of the inner loop and maybe some of your confusion:
%%%
m = 816;
bev = zeros(2508,5);
for ii = 1:2508
    avg = coup(centlon(1:816)< lim(ii,1) && (centlon(1:816) > lim(ii,2)) && (centlat(1:816) < lim(ii,3)) && (centlat(1:816) > lim(ii,4)),4);
    bev(ii,5) = sum(avg)/numel(avg);
end

Subject: grid comparison

From: David Craig

Date: 17 Nov, 2010 23:05:06

Message: 4 of 7

"Sean de " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <ic1ib4$qpu$1@fred.mathworks.com>...
> "Sean de " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <ic1h5k$a7r$1@fred.mathworks.com>...
> > "David Craig" <dcdavemail@gmail.com> wrote in message <ic1d66$g2f$1@fred.mathworks.com>...
> > > Hi all,
> > >
> > > I've need to correlate two sets of data, each set is a grid with the centres of each cell associated with a value. However the grids are different sizes, so they have a different number of cells. I am trying to find the centres from one grid that appear inside each individual cell of the other and then average them.
> > >
> > > My code......
> > >
> > > for i=1:2508
> > > count=1;
> > >
> > > for m=1:816
> > >
> > >
> > > if (centlon(m) < lim(i,1)) ...
> > > && (centlon(m) > lim(i,2)) ...
> > > && (centlat(m) < lim(i,3)) ...
> > > && (centlat(m) > lim(i,4))
> > >
> > > avg(count)=coup(m,4);
> > > count=count+1;
> > >
> > > end
> > >
> > >
> > > end
> > >
> > >
> > > bev(i,5)=(sum(avg))/length(avg);
> > >
> > >
> > >
> > > end
> > >
> > > It runs but I get some very strange results in places, i.e. values where the grids do not overlap.
> > >
> > > Any ideas????
> >
> > You reset count at the beginning of every i-loop. Is this really what you want? Are the results more what you expect when you move the count initialization outside the i-loop?
>
> I misinterpreted what the count was doing in the above. What do you mean by the grids don't overlap?
>
> This will get rid of the inner loop and maybe some of your confusion:
> %%%
> m = 816;
> bev = zeros(2508,5);
> for ii = 1:2508
> avg = coup(centlon(1:816)< lim(ii,1) && (centlon(1:816) > lim(ii,2)) && (centlat(1:816) < lim(ii,3)) && (centlat(1:816) > lim(ii,4)),4);
> bev(ii,5) = sum(avg)/numel(avg);
> end


Nice idea but matlab requires scalar values for the && operators so it doesnt work.
By the grids not overlapping I mean one covers a larger area so it contains cells that dont intersect with the smaller grid, I'm interested only in the cells that do intersect.

Subject: grid comparison

From: Sean de

Date: 18 Nov, 2010 14:49:04

Message: 5 of 7

"David Craig" <dcdavemail@gmail.com> wrote in message
> > This will get rid of the inner loop and maybe some of your confusion:
> > %%%
> > m = 816;
> > bev = zeros(2508,5);
> > for ii = 1:2508
> > avg = coup(centlon(1:816)< lim(ii,1) && (centlon(1:816) > lim(ii,2)) && (centlat(1:816) < lim(ii,3)) && (centlat(1:816) > lim(ii,4)),4);
> > bev(ii,5) = sum(avg)/numel(avg);
> > end
>
>
> Nice idea but matlab requires scalar values for the && operators so it doesnt work.
> By the grids not overlapping I mean one covers a larger area so it contains cells that dont intersect with the smaller grid, I'm interested only in the cells that do intersect.

Ahh yes, just change the && to &, the vectorized version.

I still have no idea what you mean by the grid. Could you show a (small) example of your inputs, outputs, and expected results, and how the actual outputs differ from the expected ones.

Subject: grid comparison

From: David Craig

Date: 18 Nov, 2010 19:21:04

Message: 6 of 7

"Sean de " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <ic3ed0$5a7$1@fred.mathworks.com>...
> "David Craig" <dcdavemail@gmail.com> wrote in message
> > > This will get rid of the inner loop and maybe some of your confusion:
> > > %%%
> > > m = 816;
> > > bev = zeros(2508,5);
> > > for ii = 1:2508
> > > avg = coup(centlon(1:816)< lim(ii,1) && (centlon(1:816) > lim(ii,2)) && (centlat(1:816) < lim(ii,3)) && (centlat(1:816) > lim(ii,4)),4);
> > > bev(ii,5) = sum(avg)/numel(avg);
> > > end
> >
> >
> > Nice idea but matlab requires scalar values for the && operators so it doesnt work.
> > By the grids not overlapping I mean one covers a larger area so it contains cells that dont intersect with the smaller grid, I'm interested only in the cells that do intersect.
>
> Ahh yes, just change the && to &, the vectorized version.
>
> I still have no idea what you mean by the grid. Could you show a (small) example of your inputs, outputs, and expected results, and how the actual outputs differ from the expected ones.


I got it working, just a small error with in the outer loop, avg was carrying values through from previous iteration.

for i=1:2508
    count=1;
    avg=[]; % Resets avg
    for m=1:816
        if (centlon(m) < lim(i,1)) ...
             && (centlon(m) > lim(i,2)) ...
                 && (centlat(m) < lim(i,3)) ...
                     && (centlat(m) > lim(i,4))
            
                         avg(count)=coup(m,4);
                         count=count+1;
        end
    end
    bev(i,5)=(sum(avg))/numel(avg);
end

To clarify the grid it is one grid contained inside another, something like below....
______________________________________
| / \ |
| / \ |
| / \ |
| / \ |
| / \ |
| \ \ |
| \ \ |
| \ \ |
| \ \ |
| \ \ |
| \ / |
| \ / |
| \ / |
|_______________________\/____________ |
                       
Thanks for your help

Subject: grid comparison

From: David Craig

Date: 18 Nov, 2010 19:47:03

Message: 7 of 7

"David Craig" <dcdavemail@gmail.com> wrote in message <ic3ub0$ihv$1@fred.mathworks.com>...
> "Sean de " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <ic3ed0$5a7$1@fred.mathworks.com>...
> > "David Craig" <dcdavemail@gmail.com> wrote in message
> > > > This will get rid of the inner loop and maybe some of your confusion:
> > > > %%%
> > > > m = 816;
> > > > bev = zeros(2508,5);
> > > > for ii = 1:2508
> > > > avg = coup(centlon(1:816)< lim(ii,1) && (centlon(1:816) > lim(ii,2)) && (centlat(1:816) < lim(ii,3)) && (centlat(1:816) > lim(ii,4)),4);
> > > > bev(ii,5) = sum(avg)/numel(avg);
> > > > end
> > >
> > >
> > > Nice idea but matlab requires scalar values for the && operators so it doesnt work.
> > > By the grids not overlapping I mean one covers a larger area so it contains cells that dont intersect with the smaller grid, I'm interested only in the cells that do intersect.
> >
> > Ahh yes, just change the && to &, the vectorized version.
> >
> > I still have no idea what you mean by the grid. Could you show a (small) example of your inputs, outputs, and expected results, and how the actual outputs differ from the expected ones.
>
>
> I got it working, just a small error with in the outer loop, avg was carrying values through from previous iteration.
>
> for i=1:2508
> count=1;
> avg=[]; % Resets avg
> for m=1:816
> if (centlon(m) < lim(i,1)) ...
> && (centlon(m) > lim(i,2)) ...
> && (centlat(m) < lim(i,3)) ...
> && (centlat(m) > lim(i,4))
>
> avg(count)=coup(m,4);
> count=count+1;
> end
> end
> bev(i,5)=(sum(avg))/numel(avg);
> end
>
> To clarify the grid it is one grid contained inside another, something like below....
> ______________________________________
> | / \ |
> | / \ |
> | / \ |
> | / \ |
> | / \ |
> | \ \ |
> | \ \ |
> | \ \ |
> | \ \ |
> | \ \ |
> | \ / |
> | \ / |
> | \ / |
> |_______________________\/____________ |
>
> Thanks for your help


sorry that hasnt come out properly.

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