N.B.) The center of the grid relates to (x,y) co-ordinate (0,0).
MATLAB: Collate Data in Grid
2ddatagridsummaryzonal statistics
Related Solutions
We cannot test without your data, but the approach seems fine. You could use the PDIST2 function and extract everything in one shot, but as a first approach this is fine.
I will assume that the question is about saving the points, so here is small update of your code that should (if I understand your example well) generate an Excel file with 4 columns that store Circle ID, Point ID, Point X, Point Y, Point Z:
% Prealloc array of counts, and cell array for storing relevant point parameters per circle.
counts = zeros( row, 1 ) ;output = cell( row, 1 ) ;% Initialize figure.
figure() ;hold on ;% Process circles.
for circleId = 1 : row % Plot circle.
pos = [x0(circleId)-R, y0(circleId)-R, 2*R, 2*R] ; rectangle( 'Position', pos, 'Curvature', [1 1] ) ; % Compute distance from all points to current circle center.
distances = sqrt( (x - x0(circleId)).^2 + (y - y0(circleId)).^2 ) ; % Flag and count relevant points.
isInsideCircle = distances <= R ; counts(circleId) = sum( isInsideCircle ) ; % Extract relevant parameters.
pointsId = find( isInsideCircle ) ; pointsX = x(isInsideCircle) ; % Selected Lon
pointsY = y(isInsideCircle) ; % Selected Lat
pointsZ = z(isInsideCircle) ; % Selected h
% Store in relevant output cell. We build the first column as a vector of elements equal
% to circleId, whose size matches the size of the other columns (e.g. pointsId).
output{circleId} = [circleId * ones( size( pointsId )), pointsId, pointsX, pointsY, pointsZ] ;end% Build data to export to Excel by concatenating all cells of the output cell array vertically
% into a large numeric array, converting it into a cell array, and concatenating to a first
% "header" row.
output = num2cell( vertcat( output{:} )) ;output = [{'CircleID', 'PointID', 'PointX', 'PointY', 'PointZ'}; output] ; % Export to Excel.
xlswrite( 'PointsPerCircle.xlsx', output ) ;
Note that I didn't test it, but it illustrates a possible approach. Let me know if you have any question.
I actually found now a fast solution which works quite well for me. In case someone else is facing the same problem I post it here for sake of completeness.
Basically you display the value matrix Z as an image. Luckily the image/ imagesc function allows to locate each pixel on a given axis object. A square will be centered at the given xy-location with no gaps between the pixels. This is what it looks like:
Z = Binnned_Data % matrix containing the data
X = X_Pix_Positions % vector with x-positions, size(Z,1)
Y = Y_Pix_Positions % vector with y-positions, size(Z,2)
Transparency = ones(size(Z))*0.5 % setting the alpha value of each pixel(50%)
figure(999) hold on; plot(...) % above mentioned 2D plot temp_img = imagesc(X,Y,Z,'AlphaData',Transparency);
Of course the transparency is an option but since I'm talking about an overlay ...
Another really usefull option here is to adjust the color range of the pixels by adding
clims = [a,b] % I chose a and b as min/max values in Z
imagesc(temp_img,clims);
That's pretty much it. Still, if someone already has defined a patch-object that does the same job I would be interested to see that.
best,
Stephan
Best Answer