Plot 4d surface from x, y, z, c data
37 次查看(过去 30 天)
显示 更早的评论
Hello,
I was wondering how to plot a 4d surface. I have x, y and z data (unevenly spaced), and c data (c is a function of x, y, z). I would like to plot c as a surface, with particular value of c being defined by a colour.
My x, y, z and c data are column vectors, but I don't think this is suitable for the surf(x, y, z, c) function.
Any help would be greatly appreciated.
Carol
0 个评论
采纳的回答
更多回答(2 个)
Simon Fluegge
2017-2-12
编辑:Walter Roberson
2017-2-12
I have the following data:
x=rand(1,5)
y=rand(1,10)
z=rand(1,13)
score=f(x,y,z)
I would like to plot a surface. The color of the surface is related to the score value. To each data pair of x and y I only want to plot the related z value which has the best score value.
2 个评论
Walter Roberson
2017-2-12
For example,
f = @(x,y,z) repmat(x, length(y), 1, length(z)).*2 - cos( pi * repmat(y(:), 1, length(x), length(z))) + log( repmat(reshape(z,1,1,[]), length(y), length(x), 1) );
The above was invented out of "thin air", just to have some function to work with. Notice the effort needed to get the function to produce a grid of data given only vectors. The code would be messier still if there were multiple occurrences of the variables.
The choice of ways to extend the vectors into three dimensions was driven by the inputs expected by surf(), which expects meshgrid() compatible arrays instead of ndgrid() compatible arrays.
x = sort( rand(1,5) );
y = sort( rand(1,10) );
z = sort( rand(1,13) );
score = f(x,y,z);
v = max(score, [], 3); %the best score value
subplot(1, 2, 1);
surf(x, y, v) %automatically colored according to third parameter
subplot(1, 2, 2);
levs = linspace(min(score(:)), max(score(:)), 10);
for L = levs(2:end-1); isosurface(x, y, z, score, L); end
Note: from R2016b onward, the f could be re-written as
f = @(x,y,z) x .* 2 + cos( pi .* y(:) ) + log( reshape(z,1,1,[]) );
Cosmin Constantin Popescu
2019-1-22
Thank you for the idea with the isosurface. It's exactly what I needed. For anyone interested, isosurface can be used with non-evenly spaced x,y,z coordinates.
Ryan
2014-8-5
If you want to plot a surface, you should be able to rearrange your z and c data into matrices so that they form a matrix, which would then make it suitable for the surf(x,y,z,c) function.
Example:
x = 1:10;
y = 11:20;
z = x'*y;
c = randi(10,10);
surf(x,y,z,c)
Column vectors would work well for using scatter3(x,y,z,s,c) (just use scalar s, e.g. 10) and this might be a good way to quickly visualize the data. The problem with having z as a column vector for a surface is Matlab doesn't know how they're supposed to line up, and which vertices to connect to form a continuous surface. You give it this information by passing z in as a rectangular matrix, and x and y as vectors with elements that define the lines on the surface in their respective domains.
Let us know whether your data can be shaped to fit this.
4 个评论
Mohammad Aref
2016-4-25
@Muhammad, Note that
z = x'*y;
Doesn't generate a column vector. It generates a matrix that matches the dimensions needed for the Z parameter of the surface command.
Walter Roberson
2016-4-25
If the inputs x and y are both column vectors then use
z = x * y.';
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Surface and Mesh Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!