help-octave
[Top][All Lists]

## Re: counting totals in square grids (meshgrid)

 From: Jaroslav Hajek Subject: Re: counting totals in square grids (meshgrid) Date: Mon, 14 Dec 2009 11:27:56 +0100

```On Mon, Dec 14, 2009 at 11:12 AM, Isak Delberth Davids
> I see, that last question of mine was really not needed, because value
> of grid(i,j) is exactly equal to the number of elements that are in
> the i-j-th grid. What more do I want there? My question was unclear
> and aboveall, it was unnecessary. Jaroslav, thanks for the assistance!
>

Good.

> Now back to improving the efficiency of the lines:
> for i = 1:n
>         for j = 1:n
>                 grid(i,j) = sum (i-1 <= x & x < i & j-1 <= y & y < j);
>         end
> end
>
> Do you have a suggestion?
>

You can do something like this (not tested):
i = floor (x);
j = floor (y);
mask = i > 0 & i <= n & j > 0 & j <= n;
grid = accumarray ([i,j](mask,:), 1, [n, n]);

see "help accumarray"

--
RNDr. Jaroslav Hajek
computing expert & GNU Octave developer
Aeronautical Research and Test Institute (VZLU)
Prague, Czech Republic
url: www.highegg.matfyz.cz

```