how to sort a dataset into different group and get sum of each group?

1 view (last 30 days)
Hi everyone,
Now I have a question on how to sort a dataset into different group and get sum of each group, which tortures me for sevral day. I hope to get help from you, and thanks.
An example of my dataset is shown below. There are 9 rows and 12 columns. This first and second rows are x, y coordinates and the third row is the correponding value of each (x ,y).
-1.986 -2.648 -3.310 -3.972 -4.634 -5.296 -5.958 -6.621 -7.283 -7.945 -8.607 -9.269 -9.931 -10.593 -11.255 -11.917 -12.579//
-4.515 -6.020 -7.525 -9.030 -10.536 -12.041 -13.546 -15.051 -16.556 -18.061 -19.566 -21.071 -22.576 -24.081 -25.586 -27.091//
0.002 0.003 0.003 0.004 0.004 0.005 0.005 0.005 0.006 0.006 0.006 0.007 0.007 0.007 0.007 0.008 0.008 0.008
-3.967 -5.289 -6.611 -7.934 -9.256 -10.578 -11.900 -13.223 -14.545 -15.867 -17.189 -18.512 -19.834 -21.156 -22.478 -23.801//
-5.372 -7.163 -8.954 -10.745 -12.536 -14.326 -16.117 -17.908 -19.699 -21.490 -23.280 -25.071 -26.862 -28.653 -30.443 -32.234//
0.001 0.002 0.002 0.003 0.003 0.003 0.004 0.004 0.004 0.005 0.005 0.005 0.005 0.005 0.005 0.006
-3.606 -4.808 -6.011 -7.213 -8.415 -9.617 -10.819 -12.021 -13.223 -14.425 -15.627 -16.829 -18.032 -19.234 -20.436 -21.638//
-2.758 -3.678 -4.597 -5.517 -6.436 -7.356 -8.275 -9.195 -10.114 -11.034 -11.953 -12.873 -13.792 -14.711 -15.631 -16.550//
0.002 0.003 0.003 0.004 0.005 0.005 0.006 0.006 0.006 0.007 0.007 0.007 0.008 0.008 0.008 0.008
I want to sort the data according to (x,y) coordinates into idfferent groups and get the sums of each group. In this case, the group can be
(-35<x<-30,-35<y<-30),(-30<x<-25,-35<y<-30),....(-5<x<0,-35<y<-30);
(-35<x<-30,-30<y<-25),(-30<x<-25,-30<y<-25),....(-5<x<0,-30<y<-25);
(-35<x<-30,-25<y<-20),(-30<x<-25,-25<y<-20),....(-5<x<0,-25<y<-20);
(-35<x<-30,-5<y< 0),(-30<x<-25,-5<y< 0),....(-5<x<0,-5<y<0)
Then get the sum of each group.
Thanks once again.
  2 Comments
Walter Roberson
Walter Roberson on 23 Jan 2014
Is it correct that you are wanting to distribute into a rectangular grid? If so it becomes easy. Even easier if the grid boundaries are the same distance apart.
What should be done for the case that x is exactly 30? As you have used inequalities at each stage, points on the boundaries have no-where to go.
Genhou
Genhou on 24 Jan 2014
Yes.I want to distribute into rectangular grid.
The data and the rectangular grid that I show are an examples to merely explain my question. Your suggestion is great. I would choose this way:(-35=<x<-30,-5=<y< 0),(-30=<x<-25,-5=<y=< 0),....(-5=<x<0,-5<y=<0).
Then How to get the distribution into rectangular grid?

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 24 Jan 2014
mingrid = -30;
gridgap = 5;
xgrididx = 1 + floor((x - mingrid) ./ gridgap);
ygrididx = 1 + floor((y - mingrid) ./ gridgap);
gridcounts = accumarray( [xgrididx(:), ygrididx(:)], 1 );
Now gridcounts is an array of counts.
row K is the range (K - 1) * gridgap + mingrid <= x < K * gridgap + mingrid . Same calculation for column number and y.

More Answers (0)

Categories

Find more on Shifting and Sorting Matrices in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!