Smoothing 3D Matrix
7 views (last 30 days)
Show older comments
I have a 3D surfrace plotted but it is quite rough and I need to smooth it out. I have tried using interpt2 and I keep recieving error codes.
below is the code I have been trying
x=0:21;
y=0:17;
[x,y]=meshgrid(x,y);
z=[-5 -5 0 0 0 0 0 0 0 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5;0 0 5 0 1 0 2 0 0 1 0 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5; 0 2 0 1 0 0 0 1 0 0 2 0 3 0 0 1 -5 -5 -5 -5 -5;0 0 0 0 0 1 0 0 0 0 0 2 0 0 0 0 0 -5 -5 -5 -5;-5 -5 0 2 0 0 0 0 13 0 0 0 0 0 0 0 0 0 -5 -5 -5;-5 -5 0 0 0 0 0 0 0 0 0 0 24 0 0 0 0 0 0 -5 -5;-5 -5 -5 0 0 0 0 0 0 1 0 0 24 24 0 0 0 0 0 0 -5;-5 -5 -5 -5 0 0 0 0 0 0 4 0 0 0 0 0 0 0 0 0 0;-5 -5 -5 -5 -5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -5 -5;-5 -5 -5 -5 -5 0 0 0 2 0 0 0 0 0 0 0 0 0 -5 -5 -5;-5 -5 -5 -5 -5 -5 0 0 0 0 0 0 0 0 0 0 1 0 -5 -5 -5;-5 -5 -5 -5 -5 -5 -5 0 3 0 0 0 0 0 23 0 0 -5 -5 -5 -5;-5 -5 -5 -5 -5 -5 -5 -5 0 0 0 0 0 27 27 0 -5 -5 -5 -5 -5;-5 -5 -5 -5 -5 -5 -5 -5 0 0 0 0 0 0 0 -5 -5 -5 -5 -5 -5;-5 -5 -5 -5 -5 -5 -5 -5 -5 0 0 0 0 -5 -5 -5 -5 -5 -5 -5 -5;-5 -5 -5 -5 -5 -5 -5 -5 -5 0 0 0 -5 -5 -5 -5 -5 -5 -5 -5 -5;-5 -5 -5 -5 -5 -5 -5 -5 -5 -5 0 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5];
surf(z)
xlabel('Width')
ylabel('Length')
zlabel('Number of Earthquakes')
xi=0:.1:10;
yi=0:.1:10;
[xi,yi]=meshgrid(xi,yi);
zc=interp2(x,y,z,xi,yi,'cubic');
surf(xi,yi,zc)
These are the errors that keep popping up
Error using griddedInterpolant
GridVectors must define a grid whose size is
compatible with the Values array.
Error in interp2>makegriddedinterp (line 226)
F = griddedInterpolant(varargin{:});
Error in interp2 (line 134)
F = makegriddedinterp(X, Y, V, method,extrap);
Error in MultiVaribleProject (line 12)
zc=interp2(x,y,z,xi,yi,'cubic');
0 Comments
Accepted Answer
Voss
on 18 Nov 2022
x=0:21;
y=0:17;
[x,y]=meshgrid(x,y);
z=[-5 -5 0 0 0 0 0 0 0 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5;0 0 5 0 1 0 2 0 0 1 0 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5; 0 2 0 1 0 0 0 1 0 0 2 0 3 0 0 1 -5 -5 -5 -5 -5;0 0 0 0 0 1 0 0 0 0 0 2 0 0 0 0 0 -5 -5 -5 -5;-5 -5 0 2 0 0 0 0 13 0 0 0 0 0 0 0 0 0 -5 -5 -5;-5 -5 0 0 0 0 0 0 0 0 0 0 24 0 0 0 0 0 0 -5 -5;-5 -5 -5 0 0 0 0 0 0 1 0 0 24 24 0 0 0 0 0 0 -5;-5 -5 -5 -5 0 0 0 0 0 0 4 0 0 0 0 0 0 0 0 0 0;-5 -5 -5 -5 -5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -5 -5;-5 -5 -5 -5 -5 0 0 0 2 0 0 0 0 0 0 0 0 0 -5 -5 -5;-5 -5 -5 -5 -5 -5 0 0 0 0 0 0 0 0 0 0 1 0 -5 -5 -5;-5 -5 -5 -5 -5 -5 -5 0 3 0 0 0 0 0 23 0 0 -5 -5 -5 -5;-5 -5 -5 -5 -5 -5 -5 -5 0 0 0 0 0 27 27 0 -5 -5 -5 -5 -5;-5 -5 -5 -5 -5 -5 -5 -5 0 0 0 0 0 0 0 -5 -5 -5 -5 -5 -5;-5 -5 -5 -5 -5 -5 -5 -5 -5 0 0 0 0 -5 -5 -5 -5 -5 -5 -5 -5;-5 -5 -5 -5 -5 -5 -5 -5 -5 0 0 0 -5 -5 -5 -5 -5 -5 -5 -5 -5;-5 -5 -5 -5 -5 -5 -5 -5 -5 -5 0 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5];
whos x y z
As you can see there, z has one fewer row and column than x and y. That's what the error message from interp2 is complaining about.
Defining x and y to start with 1 allows interp2 to work (I have no idea if this is an appropriate fix in this situation):
x=1:21;
y=1:17;
[x,y]=meshgrid(x,y);
z=[-5 -5 0 0 0 0 0 0 0 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5;0 0 5 0 1 0 2 0 0 1 0 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5; 0 2 0 1 0 0 0 1 0 0 2 0 3 0 0 1 -5 -5 -5 -5 -5;0 0 0 0 0 1 0 0 0 0 0 2 0 0 0 0 0 -5 -5 -5 -5;-5 -5 0 2 0 0 0 0 13 0 0 0 0 0 0 0 0 0 -5 -5 -5;-5 -5 0 0 0 0 0 0 0 0 0 0 24 0 0 0 0 0 0 -5 -5;-5 -5 -5 0 0 0 0 0 0 1 0 0 24 24 0 0 0 0 0 0 -5;-5 -5 -5 -5 0 0 0 0 0 0 4 0 0 0 0 0 0 0 0 0 0;-5 -5 -5 -5 -5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -5 -5;-5 -5 -5 -5 -5 0 0 0 2 0 0 0 0 0 0 0 0 0 -5 -5 -5;-5 -5 -5 -5 -5 -5 0 0 0 0 0 0 0 0 0 0 1 0 -5 -5 -5;-5 -5 -5 -5 -5 -5 -5 0 3 0 0 0 0 0 23 0 0 -5 -5 -5 -5;-5 -5 -5 -5 -5 -5 -5 -5 0 0 0 0 0 27 27 0 -5 -5 -5 -5 -5;-5 -5 -5 -5 -5 -5 -5 -5 0 0 0 0 0 0 0 -5 -5 -5 -5 -5 -5;-5 -5 -5 -5 -5 -5 -5 -5 -5 0 0 0 0 -5 -5 -5 -5 -5 -5 -5 -5;-5 -5 -5 -5 -5 -5 -5 -5 -5 0 0 0 -5 -5 -5 -5 -5 -5 -5 -5 -5;-5 -5 -5 -5 -5 -5 -5 -5 -5 -5 0 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5];
whos x y z % all the same size now
surf(z)
xlabel('Width')
ylabel('Length')
zlabel('Number of Earthquakes')
xi=0:.1:10;
yi=0:.1:10;
[xi,yi]=meshgrid(xi,yi);
zc=interp2(x,y,z,xi,yi,'cubic');
surf(xi,yi,zc)
However, notice that the resulting higher-resolution surface doesn't really have the same shape as the orginal. This is because the query points xi and yi only go up to 10 rather than 21 or 17 like the original x and y, respectively, so this surface only covers roughly a quarter of the original region. To fix that, define xi and yi to span the original region defined by x and y. For instance:
xi = linspace(x(1),x(end),50);
yi = linspace(y(1),y(end),50);
[xi,yi]=meshgrid(xi,yi);
zc=interp2(x,y,z,xi,yi,'cubic');
surf(xi,yi,zc)
xlabel('Width')
ylabel('Length')
zlabel('Number of Earthquakes')
Now it covers the same region as the original surface, and has ~2.5x the resolution. Adjust the 50 used in defining xi and yi to adjust the resolution.
0 Comments
More Answers (0)
See Also
Categories
Find more on Surface and Mesh Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!