3D mesh plot

9 次查看(过去 30 天)
May
May 2023-2-27
Hello, I wrote this code in order to get a 3D mesh plot based on those x,y,t values. is there any way to confirm that the plot would look like this? or i need to just trust Matlab with it? (added the plot)
%given data
x = [0 10 20 30 0 10 20 0 10 20 30];
y = [0 0 0 0 15 15 15 15 30 30 30 30];
t = [20 22 25 23 22 27 30 25 19 20 18 22];
%grid for x and y
%use unique so each value will be used once
[X,Y] = meshgrid(unique(x), unique(y));
%turning t into the same size as X and Y
T = reshape(t,length(unique(y)),length(unique(x)));
%3D plot for the values
figure;
mesh(X,Y,T);
xlabel('X');
ylabel('Y');
zlabel('T');

回答(1 个)

Star Strider
Star Strider 2023-2-27
The ‘x’ vector is missing a ‘30’ value (supplied here).
I get a different result when I plot those data —
x = [0 10 20 30 0 10 20 30 0 10 20 30];
y = [0 0 0 0 15 15 15 15 30 30 30 30];
t = [20 22 25 23 22 27 30 25 19 20 18 22];
% Q = [size(x); size(y); size(t)]
xv = linspace(min(x), max(x), numel(x));
yv = linspace(min(y), max(y), numel(y));
[X,Y] = meshgrid(xv,yv);
F = scatteredInterpolant(x(:),y(:),t(:)); % Use 'scatteredInterpolant'
T = F(X,Y);
figure
surfc(X, Y, T)
colormap(turbo)
colorbar
xlabel('X')
ylabel('Y')
zlabel('T')
[Uy,yix] = unique(y);
Rows = mean(diff(yix));
X = reshape(x,Rows,[]); % Using 'reshape'
Y = reshape(y,Rows,[]);
T = reshape(t,Rows,[]);
figure
surfc(X, Y, T) % 'surfc'
colormap(turbo)
colorbar
xlabel('X')
ylabel('Y')
zlabel('T')
figure
meshc(X, Y, T) % 'meshc'
colormap(turbo)
colorbar
xlabel('X')
ylabel('Y')
zlabel('T')
Experiment to get different results.
.

类别

Help CenterFile Exchange 中查找有关 Surface and Mesh Plots 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by