Averaging scattered data over an n by n grid
Show older comments
Hello,
I'm new to Matlab, and this is my first question here.
I'm working with a scattered dataset. I have a long array of height values, as well as two equally long arrays for the x and y coordinates per height value. So, point 1 has a height of z(1) and is located at x(1), y(1), and so forth. All xy values fall within a range of xstart:xend and ystart:yend.
I'd like to reduce the amount of values I have by taking the average over a smaller n by n grid. Since the data is scattered, it is possible no datapoints fall within a grid cell. In that case I'd like the cell value to be set to 0, instead of NaN. So the new arrays would be the average height per cell in a znew array, and the cell center coordinates in the xnew and ynew arrays. Just to be clear, I've illustrated what I want in the figure below:

What is the best way to do this? Thanks in advance.
1 Comment
dpb
on 23 Jul 2021
See histcounts2 with the output arguments of
[~,~,~,ix,iy]=histcounts2(x,y,xEdges,yEdges);
Then average heights(ix,iy) for each pair ix, iy
Accepted Answer
More Answers (0)
Categories
Find more on Scatter Plots in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!