Counitng the number of scatter points above a surface

Hi All,
I am having some troubles and hoping someone out there much smarter with MATLAB than me can help.
The problem I have i
s;
  • I have plotted some data as a 3D scatter plot.
  • I have then plotted a surface which is a ‘limiting condition’ for the data I have.
  • I need to count the number of data points that fall above and below the surface.
Is this something that can be done?
I have been reading up on slices and searching these forums for help but cant find anything close.
Any help is appreciated.
The code is;

 Accepted Answer

You can use interp2 to evaluate the surface at the X & Y coordinates of your scatter data.
Consider the following example:
% Make up some sample data
[sx,sy,sz] = peaks(12);
px = randn(1,100);
py = randn(1,100);
pz = 5*randn(1,100);
% draw the surface and points
h1 = surf(sx,sy,sz);
hold on
h2 = scatter3(px,py,pz,'filled');
% Evaluate surface's function @ point's X & Y
spz = interp2(sx,sy,sz, px,py);
% Build mask of points which are above surface
mask = pz > spz;
% Delete old scatter
delete(h2)
% Replace with two scatters with different colors
h3 = scatter3(px(mask),py(mask),pz(mask),'filled');
h4 = scatter3(px(~mask),py(~mask),pz(~mask),'filled');
h3.MarkerFaceColor = 'red';
h4.MarkerFaceColor = 'blue';
But you should read this blog post to understand some of the subtleties of how surface does it's interpolation before you go too far down this path.

2 Comments

Thats great - Thanks Mike!
Very much appreciated.
Mike Garrity, is it possible to make a surface pace above for all points?

Sign in to comment.

More Answers (1)

Can your surface be expressed as z = f(x,y)? If so for every scatter point (at position (x0,y0, z0)), you can do:
h = z0 - f(x0,y0)
If h > 0 your point is above the surface.
If you can't express your surface as z = f(x,y), you can use interp2 or something to get an approximation to z at any (x,y) and do the same as above.

3 Comments

Hi Lukas, Thanks for the quick response!
I am still confused however as I cant express z=f(x,y).
Some more background;
  • z is is a series of test results as a [8 x 16] matrix.
  • I have used interp2 to create zq as a [526 x 301].
  • My scatter data is (X,Y,Z) with 46752 points!
I understand the principle of your suggestion and it seems to make sense, but I dont get how to execute it. If I want to check h > 0, I need to lookup Z value from my scatter data, and then use zq approximation for corresponding X and Y value right??
How can I do this if zq which I created using interpt2 is a [526 x 301] matrix with no real order to it.
Thanks for the help. I really appreciate it.
You could try to use scatteredInterpolant to evaluate all your points. The process might look something like this:
F = scatteredInterpolant(x, y, z) % x, y and z can be unordered
Then for each point you could call:
z_surface = F(point_x, point_y);
Although this may take a while to run for 46752 points. If you're planning on doing this several times that might be an issue.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!