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'