# Collate Data in Grid

22 views (last 30 days)
Si on 14 May 2014
Commented: Image Analyst on 14 Jun 2018

I have a list of data points in 2 dimensions (x and y co-ordinates). They fall within a certain range, +R and -R. There is also 3rd (variable) property connected with each data point. i.e., The list contains 3 columns. The first two column entries relate to the position. The third column relates to speed. There are approximately 1,000,000 rows of data.

1. I want to set up an 'imaginary' grid to count the number of points which fall in each grid location based on the (x, y) location. The size of the grid can be varied. I would like to also plot this data (A hot spot type map showing x, y position with color indicating the count number at each location).

2. Then I also want to be able to sort the data within each grid (or any particular specified grid). I want to be able to sort through the data relating to an individual grid and count the number of points which have the same/different speed. Then I would also like to plot this (from data within a specified grid location: count versus speed).

Any ideas would be welcome on how to implement this.

thanks

How can I do this? Any ideas?

thanks

Image Analyst on 14 Jun 2018
Show what code you did, what you got, and what you want, in a new discussion all your own.

Cedric Wannaz on 17 May 2014
Edited: Cedric Wannaz on 17 May 2014
In addition to Image Analyst's hints, you could also look at
Alternatively, here is a short example which illustrates how to grid data "by hand" on a regular grid. It can be adapted to irregular grids, to produce what is sometimes named zonal statistics in GIS contexts.
R = 4 ;
% - Define dummy data.
n = 15 ;
x = -R + 2*R*rand( n, 1 ) ;
y = -R + 2*R*rand( n, 1 ) ;
v1 = randi( 20, n, 1 ) ; % A series of data associated with points.
v2 = randi( 20, n, 1 ) ; % Another series.
% - Build grid.
nBinsX = 3 ;
nBinsY = 2 ;
xg = linspace( -R, R, nBinsX+1 ) ;
yg = linspace( -R, R, nBinsY+1 ) ;
nCells = nBinsX * nBinsY ;
% - Build figure.
figure(1) ; clf ; hold on ;
set( gcf, 'Color', 'w', 'Units', 'Normalized', ...
'Position', [0.1,0.1,0.6,0.6] ) ;
% - Plot grid.
plot( [xg;xg], repmat( [-R;R], 1, numel( xg )), 'Color', 0.8*[1,1,1] ) ;
plot( repmat( [-R;R], 1, numel( yg )), [yg;yg], 'Color', 0.8*[1,1,1] ) ;
xlim( 1.5*[-R,R] ) ; ylim( 1.5*[-R,R] ) ;
% - Build set of unique IDs for cells.
xId = sum( bsxfun( @ge, x, xg(1:end-1) ), 2 ) ;
yId = sum( bsxfun( @ge, y, yg(1:end-1) ), 2 ) ;
cellId = nBinsY * (xId - 1) + yId ;
% - Plot cell IDs.
labels = arrayfun( @(k)sprintf( '%d', k ), 1:nCells, 'UniformOutput', false ) ;
[X,Y] = meshgrid( (xg(1:end-1)+xg(2:end))/2, (yg(1:end-1)+yg(2:end))/2 ) ;
text( X(:), Y(:), labels, 'Color', 'b', 'FontSize', 14 ) ;
% - Plot data points with labels.
plot( x, y, 'rx', 'LineWidth', 2, 'MarkerSize', 8 ) ;
labels = arrayfun( @(k)sprintf( 'P%d\\in%d | %d,%d', k, cellId(k), ...
v1(k), v2(k) ), 1:n, 'UniformOutput', false ) ;
text( x, y+R/100, labels, 'Color', 'k', 'FontSize', 9, ...
'HorizontalAlignment', 'Center', 'VerticalAlignment', 'Bottom' ) ;
% - Compute some stat (sum, mean) per block on v1 and v2.
blockSum_v1 = accumarray( cellId, v1, [nCells, 1] ) ;
blockMean_v2 = accumarray( cellId, v2, [nCells, 1], @mean ) ;
fprintf( '\nBlock sum v1 =\n' ) ;
disp( blockSum_v1 ) ;
fprintf( '\nBlock mean v2 =\n' ) ;
disp( blockMean_v2 ) ;
This outputs
and
Block sum v1 =
25
58
9
24
28
28
Block mean v2 =
17.5000
10.0000
6.5000
7.5000
13.0000
11.5000
Note that if you eliminate all the "demo" code, the approach reduces to almost a one-liner for defining cellIDs and then a one-liner per stat.
Cedric Wannaz on 17 May 2014
Ok. Yes, then it is easy to isolate a block with basic indexing:
isSelected = cellId == 5 ;
blockData = v1(isSelected) ;
or simply
blockData = v1(cellId==5) ;
which is I guess what you did. You can also put all blocks of data is a cell array:
>> v1byCell = accumarray( cellId, v1, [nCells, 1], @(x){x} )
v1byCell =
[3x1 double]
[4x1 double]
[2x1 double]
[2x1 double]
[3x1 double]
[ 1]

Image Analyst on 15 May 2014
1. Try griddedInterpolant, TriScatteredInterp, or griddata, depending on the version of MATLAB you're using.
2. Try sort() and hist() or histc().
Si on 17 May 2014

Sudhanshu Soman on 23 Feb 2016
Thank you so much Cedric.
I have some particle data for which I placed grid using your suggested code and with minor corrections. I could get the figure which is attached below. Q-Instead of rectangular grid, is it possible to place the circular grid on it ? As you can the grid is rectangular and due to which some grid cells are falling outside the circle. These cells have no data points. If I can place the circular grid then these empty cells won't be there ( that's what I want).
Q- If the circular grid is not possible to place on it, then can you please suggest me other way to remove these empty cells.
I tried using 'nan' to remove them, but the only problem is it will also remove the empty cells which are inside the circle. I just want to remove the empty cells which are 'outside' circular particle data (as you can see I have marked the outside one), not the inside one.
Any help will be highly appreciated. Thank you so much.
My particle data is in term of circular plane only.