0
votes

I have a point set with (x,y) coordinates and their corresponding weights in matrix a where the 1st, 2nd and 3rd columns are x, y, and weight respectively. I want to divide this point set into grid cells, and count the number of points in each grid and the total weight of each grid.

I tried the small example below, but it did not work. Here I tried to divide this data set into a 2x2 small grid and tried to count number of points and their sum of weights. Further, I have big data set, so I can not extend this approach further when I need different step sizes for grid.

Can someone please help me to develop an easier approach?

function dataTree
count=zeros(9,1);
avg=zeros(9,1);
data=[1 3 100; 2 1 120; 3 5 110; 4 2 100; 5 3 150; 6 2 100];

for i=1:6
    if data(i,1)<=2
        for j=1:6
            if data(j,2)<=2
                count(1) = count(1) + 1;
                avg(1) = avg(1) + data(j,3);
            elseif data(j,2)<=4
                    count(2) = count(2) + 1;
                    avg(2) = avg(2) + data(j,3);
             elseif data(j,2)<=6
                    count(3) = count(3) + 1;
                    avg(3) = avg(3) + data(j,3);
            end
        end
    elseif data(i,1)<=4
        for j=1:6
            if data(j,2)<=2
                count(4) = count(4) + 1;
                avg(4) = avg(4) + data(j,3);
            elseif data(j,2)<=4
                    count(5) = count(5) + 1;
                    avg(5) = avg(5) + data(j,3);
             elseif data(j,2)<=6
                    count(6) = count(6) + 1;
                    avg(6) = avg(6) + data(j,3);
            end
        end
    elseif data(i,1)<=6
        for j=1:6
            if data(j,2)<=2
                count(7) = count(7) + 1;
                avg(7) = avg(7) + data(j,3);
            elseif data(j,2)<=4
                    count(8) = count(8) + 1;
                    avg(8) = avg(8) + data(j,3);
             elseif data(j,2)<=6
                    count(9) = count(9) + 1;
                    avg(9) = avg(9) + data(j,3);
            end
        end

    end
end
count'
avg'
1

1 Answers

1
votes

If your x and y are not yet rounded to some arbitrary units, do so first:

x = round((x - min(x))/edgelength+1);

this makes sure that you obtain grid with edgelength sized squares, which is indicated by non-zero integers. Do the same for y.

Then you can use either sparse or accumarray to get the total weight. sparse is faster, but is less wide applicable:

gridWeight = sparse(x,y,weight);

if you want to get average weights, add 1 for each entry and divide by that matrix:

NumEntries = sparse(x,y,1);
MeanWeights = gridWeight./NumEntries;

accumarray can do both of these operations in one go:

gridWeight = accumarray([x y],weight);
MeanWeights = accumarray([x y], weight,[],@mean); %//add ,[], 'issparse' for sparse matrix

Note that sparse is a sub-functionality of accumarary by setting accumarray=([i,j],val,[],@sum,[],'issparse'). The only function sparse can handle is @sum and it's sole fill-value is 0, whilst for accumarray other functions and values can be used.