How to remove outliers in a 3D surface

24 views (last 30 days)
HAMID
HAMID on 6 Nov 2021
Commented: Chris on 7 Nov 2021
I have a 3D surface (see below) construced using surf command based on excel data:
T = readtable('https://in.mathworks.com/matlabcentral/answers/uploaded_files/791474/excel-surface.xlsx')
x = T.(1) ;
y = T.(2) ;
z = T.(3) ;
xi = linspace(min(x),max(x));
yi = linspace(min(y),max(y)) ;
[X,Y] = meshgrid(xi,yi) ;
Z = griddata(x,y,z,X,Y) ;
surf(X,Y,Z)
shading interp
colorbar
The resulting surface is as follows:
However, I know that the surface should be smooth without these "peaks" that appear randomly from above or below.
Is there a way to remove these outliers and interpolate to construct a smooth surface? (NOTE, I AM NEW TO MATLAB).

Answers (2)

Matt J
Matt J on 6 Nov 2021
You could try rmoutliers().
  5 Comments
Matt J
Matt J on 6 Nov 2021
I would have to see what you tried in order to be able to guess what's not working.

Sign in to comment.


Chris
Chris on 6 Nov 2021
Edited: Chris on 6 Nov 2021
I would recommend trying the "clean outlier data" task in the live editor, after sorting the data:
T = sortrows(T,1); % Sort by the first column ('x')
Open a New Live Script, and select the task.
Select the table and input variable z.
I can get decent results with spline interpolation, a centered moving median, and a low threshold. But I'm not sure how, computationally, you would create a perfectly smooth surface that retains the contours you would want to see, as there are quite a few clumped outliers.
Perhaps repeated sorting by x and y, with outlier cleaning in between, could work.
  2 Comments
HAMID
HAMID on 7 Nov 2021
clear; clc;
% Read table:
T = readtable('https://in.mathworks.com/matlabcentral/answers/uploaded_files/791474/excel-surface.xlsx')
% Fill outliers:
T = sortrows(T,1);
T.Z = filloutliers(T.Z, "spline", "movmedian", 4);
T = sortrows(T,2);
T.Z = filloutliers(T.Z, "spline", "movmedian", 4);
% Plot surface:
x = T.(1) ;
y = T.(2) ;
z = T.(3) ;
xi = linspace(min(x),max(x));
yi = linspace(min(y),max(y)) ;
[X,Y] = meshgrid(xi,yi) ;
Z = griddata(x,y,z,X,Y) ;
surf(X,Y,Z)
shading interp
colorbar
I tried sorting multiple times but it is not working. I now have a new weird outlier.
Before:
After:
Chris
Chris on 7 Nov 2021
clear; clc;
% Read table:
T = readtable('https://in.mathworks.com/matlabcentral/answers/uploaded_files/791474/excel-surface.xlsx');
% Fill outliers:
for idx = 1:2
for jdx = 8:-2:2
T = sortrows(T,1);
T.Z = filloutliers(T.Z, "spline", "movmedian", jdx);
T = sortrows(T,2);
T.Z = filloutliers(T.Z, "spline", "movmedian", jdx);
end
end
% Plot surface:
x = T.(1) ;
y = T.(2) ;
z = T.(3) ;
xi = linspace(min(x),max(x));
yi = linspace(min(y),max(y)) ;
[X,Y] = meshgrid(xi,yi) ;
Z = griddata(x,y,z,X,Y) ;
surf(X,Y,Z)
% shading interp
colorbar
Getting pretty close.
With default shading, you'll see there are still a few bumps. But you can also see interesting features. Repeating the process, and changing the window size, continues to smooth the surface.

Sign in to comment.

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!