简体   繁体   中英

MATLAB: fitting a 3d surface to a bar graph

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.

% 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);

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM