Reputation: 297
I have a matrix (about 400x400) of numbers between 0 and 1. Here is a plot of the 3D bar graph
I want to fit a 3D surface to the bar graph. Any ideas? The elements of the matrix (the numbers between 0 and 1) should give the height of the surface at each index. I would like the surface to just give us the general shape of the bar graph, and not go through every point.
Upvotes: 0
Views: 443
Reputation: 166
% init
x = randn(1000,1);
y = randn(1000,1);
nbins = [10 20];
% make histogram
h = histogram2(x,y,nbins)
% set limits and steps
min_x = min(x); max_x = max(x); step_x = (max_x - min_x)/nbins(1);
min_y = min(y); max_y = max(y); step_y = (max_y - min_y)/nbins(2);
% make grid
surf_z = h.Values;
surf_x = [min_x + step_x/2 : step_x : max_x - step_x/2];
surf_y = [min_y + step_y/2 : step_y : max_y - step_y/2];
[xx, yy] = meshgrid(surf_x, surf_y)
% plot 3D surface
surf(xx',yy',surf_z)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% other variant
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% make grid and interpolant
[xx, yy] = ndgrid(surf_x, surf_y)
F = griddedInterpolant(xx,yy,surf_z,'spline');
% make 3D surface with a some step value
step = 0.01;
[Xq,Yq] = ndgrid(min(surf_x):step:max(surf_x), min(surf_y):step:max(surf_y));
Zq = F(Xq,Yq);
% plot 3D surface
mesh(Xq,Yq,Zq);
Upvotes: 1