MATLAB-Surf Plot数据结构 [英] MATLAB - Surf Plot data structure

查看:192
本文介绍了MATLAB-Surf Plot数据结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用2种不同的方法进行了计算.对于这些计算,我改变了2个参数:x和y

I have done calculations with 2 different methods. For those calculations I have varied 2 parameters: x and y

最后,对于每种变化形式,我都计算了两种方法之间的%误差. 现在,我想根据结果创建3D表面图:

In the end, I have calculated the % ERROR between both methods, for each variation. Now I want to create a 3D surface plot from the results:

x -> on x axis
y -> on y axis
Error -> on z axis

以下是数据示例:

A = [
                   -0.1111                     1.267          9.45680081826912
                   -0.1111                       2.6          212.361735695025
                     -0.25                     1.533          40.5729362609655
                     -0.25                     2.867          601.253624894196
                   -0.4286                         1          0.12116749607863
                   -0.4286                       3.4          79.6948438921078
                   -0.6667                     2.067          33.3495544017519
                   -0.6667                     3.667          141.774875517481
                        -1                       2.6       -0.0399171449531781
                   0.09091                     1.533            163.7083541414 ];

但是,当我尝试使用surf函数对其进行绘制时:

But, when I try to plot it with surf function:

x = A(:,1);
y = A(:,2);
z = A(:,3);

surf(x,y,z)

,我得到一个错误:

Error using surf (line 75)
Z must be a matrix, not a scalar or vector
Error in ddd (line 27)
surf(x,y,z)

您能帮我提供一个代码,该代码可以以surf函数可接受的格式重组数据吗?

Can you help me with a code that can restructure the data in the format acceptable for the surf function?

P.S. -我目前正在尝试尝试编写一些示例代码.一旦到达某个地方,我就会将其发布.

P.S. - I am currently trying to write some sample code with my first attempts. I will post it as soon as I get to somewhere.

推荐答案

surf 函数需要一个X,Y值的网格作为输入.但是,您的数据只是带有某些组合的三个向量,而不是完整的网格.如文档中所述, meshgrid 函数通常有助于创建此类网格矩阵.使用unique函数选择xy中的所有唯一值,并创建所有可能组合的矩阵:

The surf function needs a grid of X,Y-values as input. Your data however is simply three vectors with some combinations, not a full grid. As described in the documentation, the meshgrid function is often helpful to create such grid matrices. Use the unique function to select all unique values in x and y and create matrices of all possible combinations:

[X,Y] = meshgrid(unique(x),unique(y));

要创建适合[X,Y]网格的Z矩阵,请 griddata 功能很有帮助:

To create a Z matrix which fits the [X,Y] grid, the griddata function is helpful:

Z = griddata(x,y,z,X,Y);

现在,您可以使用网格矩阵作为输入来调用surf:

Now you can call surf with the grid matrices as input:

surf(X,Y,Z);

这篇关于MATLAB-Surf Plot数据结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆